好吧,我认为这有点棘手,我有一个表单,我想用它来允许用户上传文件,当他们点击提交时,将文件通过电子邮件发送给某人。我有这个为一个文件工作。
查看代码
<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 = "your@email.co.uk";
$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);
?>
当我想附加多个文件时,问题就来了。我要按什么顺序进行操作,在第一页上我想要它,这样当您单击附加文件时,它将添加一个新文件框,然后当再次单击它时,会出现另一个文件框,依此类推。所以表单是动态创建的。
这将带我进入下一个问题,即发送附件,因为我们不知道用户附加了多少。
我确定任何指针都不会像将 if (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@email.co.uk";
$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 吗?