问题描述:

I am putting together a PHP mail based on input from a form.

On the actual form I have this for file inputs:

<input type="file" name="fileToUpload[]">

For the email I am looking to have a label such as 'attached files' and then list all the uploaded files as urls.

example: If file1.jpg was uploaded, the output in the email would be www.myurl.co.uk/file1.jpg

Note the awful way I have tried to do this by setting $target_file1 for input 1 etc.

Apart from being awful code, the problem is if for example input 2 has no file, the email output will still show the first part of the url 'http://www.myurl.co.uk/uploads/'

What is the best way to take multiple file inputs and list them all as urls in an email?

<?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 = "[email protected]";

$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._%-][email protected][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

}

?>

网友答案:

I think that the best way is upload all files, add in each iteration to an array the file uploaded, and then you'll obtain an array with all files uploaded (1 or 200, doesn't matter).

That code is an idea, don't copy&paste it.

  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>";
  }
相关阅读:
Top