好吧,我想我有一個表格,我想用它來允許用戶上傳文件,當他們點擊提交時,它會將文件發送給某人。我有這個工作的一個文件。用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了?
將大代碼塊發佈到註釋中永遠不會很好。如果你還在掙扎,隨時編輯你的問題,我可以編輯我的答案 – juco 2013-03-22 13:08:33
謝謝編輯問題 – Greyhounddad 2013-03-22 13:09:30