少しトリッキーだと思いますが、ユーザーがファイルをアップロードできるようにするために使用したいフォームがあり、送信をクリックするとファイルが誰かに電子メールで送信されます。私はこれを1つのファイルで機能させています。
コードを見る
<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 ではありませんか?