我正在根据表单的输入汇总PHP邮件。
在实际表格中,我有这个用于文件输入:
<input type="file" name="fileToUpload[]">
对于电子邮件,我希望有一个标签,如“附件”,然后将所有上传的文件列为网址。 示例:如果上传了file1.jpg,则电子邮件中的输出将为www.myurl.co.uk/file1.jpg
注意我通过为输入1等设置$ target_file1来尝试这样做的可怕方式。 除了糟糕的代码,问题是如果例如输入2没有文件,电子邮件输出仍将显示网址的第一部分“http://www.myurl.co.uk/uploads/”
获取多个文件输入并将其全部列为电子邮件中的网址的最佳方法是什么?
<?php
for($i=0; $i<count($_FILES['fileToUpload']['name']); $i++) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"][$i]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"][$i], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"][$i]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
$target_file1 = "http://www.myurl.co.uk/uploads/" . basename($_FILES["fileToUpload"]["name"][0]);
$target_file2 = "http://www.myurl.co.uk/uploads/" . basename($_FILES["fileToUpload"]["name"][1]);
$target_file3 = "http://www.myurl.co.uk/uploads/" . basename($_FILES["fileToUpload"]["name"][2]);
$target_file4 = "http://www.myurl.co.uk/uploads/" . basename($_FILES["fileToUpload"]["name"][3]);
$target_file5 = "http://www.myurl.co.uk/uploads/" . basename($_FILES["fileToUpload"]["name"][4]);
$target_file6 = "http://www.myurl.co.uk/uploads/" . basename($_FILES["fileToUpload"]["name"][5]);
if(isset($_POST['email'])) {
$email_to = "test@test.com";
$email_subject = "A customer has submitted their files";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['ref_no']) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$ref_no = $_POST['ref_no']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "A customer has submitted their files. Details are below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n\n";
$email_message .= "Email: ".clean_string($email)."\n\n";
$email_message .= "RefNo: ".clean_string($ref_no)."\n\n";
$email_message .= "Attached Files: \n".clean_string($target_file1)."\n\n".clean_string($target_file2)."\n\n".clean_string($target_file3)."\n\n".clean_string($target_file4)."\n\n".clean_string($target_file5).";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Your file(s) have been submitted successfully.
<?php
}
?>
答案 0 :(得分:1)
我认为最好的方法是上传所有文件,在每次迭代中添加上传文件的数组,然后您将获得一个包含所有文件上传的数组(1或200,无关紧要) )。
该代码是一个想法,不要复制和粘贴它。
foreach($_FILES['fileToUpload'] as $key => $file) {
move_uploaded_file(....);
$array[] = $pathToFile;
}
$html = '';
foreach($array as $singleFile) {
$html .= "<a href="">http://www.yourdomain.com/".$singleFile."</a><br>";
}