用php发送附件附件

问题描述:

好吧,我想我有一个表格,我想用它来允许用户上传文件,当他们点击提交时,它会将文件发送给某人。我有这个工作的一个文件。用php发送附件附件

见代码

<html> 
<head> 
    <title>Administration - upload new files</title> 
</head> 
<body> 
<h1>Upload new news files</h1> 
<form enctype="multipart/form-data" action="upload.php" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
    Upload this file: <input name="userfile" type="file"> 

    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
    Upload this file: <input name="userfile" type="file"> 
    <input type="submit" value="Send File"> 
</form> 
</body> 
</html> 

<html> 
<head> 
    <title>Uploading...</title> 
</head> 
<body> 
<h1>Uploading file...</h1> 
<?php 
$youremail = "[email protected]"; 
$tmp_name = $_FILES['userfile']['tmp_name']; 
    $type = $_FILES['userfile']['type']; 
    $name = $_FILES['userfile']['name']; 
    $size = $_FILES['userfile']['size']; 
    if (file_exists($tmp_name)) 
     { 
     if(is_uploaded_file($tmp_name)) { 
      $file = fopen($tmp_name,'rb');   //open the file 
       $data = fread($file,filesize($tmp_name)); //read the file 
       fclose($file);     // close the file 
       $data = chunk_split(base64_encode($data)); // encode and split 
       } 
      $bound_text = "x".md5(mt_rand())."x"; 
      $bound = "--".$bound_text."\r\n"; 
      $bound_last = "--".$bound_text."--\r\n"; 
      $headers = "From: {$sendersname}<{$sendersemail}>\r\n" 
      ."MIME-Version: 1.0\r\n" 
       ."Content-Type: multipart/mixed; boundary=\"$bound_text\""; 
      $message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n" 
       .$bound; 

      $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" 
       ."Content-Transfer-Encoding: 7bit\r\n\r\n" 
       .$sendcontent."\r\n" 
       .$bound; 

      $message .= "Content-Type: ".$type."; name=\"".$name."\"\r\n" 
       ."Content-Transfer-Encoding: base64\r\n" 
       ."Content-disposition: attachment; file=\"".$name."\"\r\n" 
       ."\r\n" 
       .$data 
       .$bound_last; 
      } 

    mail($youremail, $subject, $message, $headers); 
    ?> 

的问题来了,当我想要附加多个文件。 和我在第一页上我想要什么顺序,以便当你点击attatch文件时,它会添加一个新的文件框,然后再次点击时会出现另一个文件,等等。所以表单是动态创建的。

这会带我到下一个将发送附件的问题,因为我们不知道用户有多少附件。

任何指针作为即时通讯确保其不会像改变,如果(file_exists($ tmp_name的值),以一段时间,因为容易吗?

非常感谢你提前

编辑所以现在我有

<form enctype="multipart/form-data" action="upload.php" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
    Upload this file: <input name="userfile[]" type="file"> 

    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
    Upload this file: <input name="userfile[]" type="file"> 
    <input type="submit" value="Send File"> 

<html> 
<head> 
    <title>Uploading...</title> 
</head> 
<body> 
<h1>Uploading file...</h1> 
<?php 
$youremail = "[email protected]"; 

$i = count($_FILES['userfile']); 


foreach($_FILES['userfile'] as $file){  
    $tmp_name = $file['tmp_name']; 
    $type = $file['type']; 
    $name = $file['name']; 
    $size = $file['size']; 


    if (file_exists($tmp_name)) 
     { 
     if(is_uploaded_file($tmp_name)) { 
      $file = fopen($tmp_name,'rb');   //open the file 
       $data = fread($file,filesize($tmp_name)); //read the file 
       fclose($file);     // close the file 
       $data = chunk_split(base64_encode($data)); // encode and split 
       } 
      $bound_text = "x".md5(mt_rand())."x"; 
      $bound = "--".$bound_text."\r\n"; 
      $bound_last = "--".$bound_text."--\r\n"; 
      $headers = "From: {$sendersname}<{$sendersemail}>\r\n" 
      ."MIME-Version: 1.0\r\n" 
       ."Content-Type: multipart/mixed; boundary=\"$bound_text\""; 
      $message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n" 
       .$bound; 

      $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" 
       ."Content-Transfer-Encoding: 7bit\r\n\r\n" 
       .$sendcontent."\r\n" 
       .$bound; 

      $message .= "Content-Type: ".$type."; name=\"".$name."\"\r\n" 
       ."Content-Transfer-Encoding: base64\r\n" 
       ."Content-disposition: attachment; file=\"".$name."\"\r\n" 
       ."\r\n" 
       .$data 
       .$bound_last; 
      } 
echo "$i"; 
     } 

    mail($youremail, $subject, $message, $headers); 


    ?> 

它发送电子邮件,但没有附件,我把$ i看看发生了什么,如果只有2个文件上传,返回5就没有2了?

你可以改变你的文件输入到一个数组是这样的:

<input name="userfile[]" type="file"> 

这将额外的深度添加到您的$_FILES阵列。您可以确定上传的附件数量为count($_FILES['userfile'])。并通过提供的附件循环:

foreach($_FILES['userfile'] as $file) { 
    // Access the elements with: 
    // $file['name'] 
    // etc.. 
} 

至于点击一个链接来创建任意数量的文件输入,您将需要使用Javascript。这可以用vanilla JS来完成,但是如果您使用的是jQuery,您可能需要查看clone()

+0

将大代码块发布到注释中永远不会很好。如果你还在挣扎,随时编辑你的问题,我可以编辑我的答案 – juco 2013-03-22 13:08:33

+0

谢谢编辑问题 – Greyhounddad 2013-03-22 13:09:30