我觉得有点棘手,我有一个表单,我想用它来允许用户上传文件,当他们点击提交时,它会将文件通过电子邮件发送给某人。我有一个文件工作。
参见代码
<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);
?>
当我想要附加多个文件时,问题就出现了。 以及我在第一页上要做什么命令,这样当你点击attatch文件时它会添加一个新的文件框,然后当再次点击它时会出现另一个,依此类推。因此表单是动态创建的。
这将带我进入下一个发送附件的问题,因为我们不知道用户已经连接了多少。
任何指针都确定它不会像将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?
答案 0 :(得分: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()。