我正在根据表单的输入整理PHP邮件。
在实际表单上,我有这个文件输入:
<input type="file" name="fileToUpload[]">
对于电子邮件,我希望有一个标签,例如"附件",然后将所有上传的文件列为 url。示例:如果上传了 file1.jpg,则电子邮件中的输出将 www.myurl.co.uk/file1.jpg
请注意,我试图通过为输入 1 等设置 $target_file1 来做到这一点的糟糕方式。除了糟糕的代码之外,问题是,例如,如果输入 2 没有文件,电子邮件输出仍将显示 url 的第一部分"http://www.myurl.co.uk/uploads/"
获取多个文件输入并将它们全部列为电子邮件中的 url 的最佳方法是什么?
<?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
}
?>
我认为最好的方法是上传所有文件,将每次迭代添加到上传的文件的数组中,然后您将获得一个包含所有上传文件的数组(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>";
}