0

I'm working on a multiple files upload script and i want to be warned when users upload some files. Here is my code for the upload :

if(isset($_POST['submit_image'])){
   for($i=0;$i<count($_FILES["upload_file"]["name"]);$i++){
    $uploadfile=$_FILES["upload_file"]["tmp_name"][$i];
    $folder="galerie/evg/";

    move_uploaded_file($_FILES["upload_file"]["tmp_name"][$i], "$folder".$_FILES["upload_file"]["name"][$i]);

    $to = "benoit@lalilou.com";
    $subject = "Nouvelles photos uploadées";
    $message = "
        <html>
            <head>
                <title>".$userRow['user_firstname']." a uploadé de nouvelles photos.</title>
            </head>
            <body>
                ".$userRow['user_firstname']." a uploadé de nouvelles photos.
                ".$_FILES["upload_file"]["name"][$i]."
                <br/>
                <br/>
            </body>
        </html>
    ";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: <webmaster@lalilou.com>' . "\r\n";

}
mail($to,$subject,$message,$headers);
exit();}

My problem : I receive the mail but in the message, i only have the last file uploaded. And if i put the mail function into the for loop, i receive 2 or 3 mail depending of the amount of pictures uploaded.

My question : What is the right method to do that?

Thanks in advance :)

****EDIT****

Thanks to @menaka Here is the working final code for those who are looking for :

if(isset($_POST['submit_image'])){
$to = "benoit@lalilou.com";
$subject = "Nouvelles photos uploadées";
$message =  "
    <html>
        <head>
            <title>".$userRow['user_firstname']." a uploadé de nouvelles photos.</title>
        </head>
        <body>".$userRow['user_firstname']." a uploadé de nouvelles photos.<br/>";
for($i=0;$i<count($_FILES["upload_file"]["name"]);$i++){
    $uploadfile=$_FILES["upload_file"]["tmp_name"][$i];
    $folder="galerie/evg/";
    move_uploaded_file($_FILES["upload_file"]["tmp_name"][$i], "$folder".$_FILES["upload_file"]["name"][$i]);
    $message .= "<img style='width:200px;' src='http://exemple.com/galerie/".$_FILES["upload_file"]["name"][$i]."' />";
}
$message .= "<br/>
            <br/>
        </body>
    </html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster@lalilou.com>' . "\r\n";
mail($to,$subject,$message,$headers);
exit();}
Coldvibe
  • 11
  • 4

2 Answers2

0

I think the problem is, you are just keep replacing the last message within the loop, try out like this.

if(isset($_POST['submit_image'])){

    $to = "benoit@lalilou.com";
    $subject = "Nouvelles photos uploadées";
    $message =  "
        <html>
            <head>
                <title>".$userRow['user_firstname']." a uploadé de nouvelles photos.</title>
            </head>
            <body>";

   for($i=0;$i<count($_FILES["upload_file"]["name"]);$i++){
    $uploadfile=$_FILES["upload_file"]["tmp_name"][$i];
    $folder="galerie/evg/";

    move_uploaded_file($_FILES["upload_file"]["tmp_name"][$i], "$folder".$_FILES["upload_file"]["name"][$i]);


    $message .= $userRow['user_firstname']." a uploadé de nouvelles photos.
                ".$_FILES["upload_file"]["name"][$i];


}

$message .= "<br/>
                <br/>
            </body>
        </html>
    ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster@lalilou.com>' . "\r\n";

mail($to,$subject,$message,$headers);
exit();}
menaka
  • 1,045
  • 1
  • 12
  • 30
-1

You have to load each file and add it to the filename header. I think that will answer to you question : How to attach two or multiple files and send mail in PHP

khalidgt
  • 1
  • 1
  • That's a way but if i can get the images into the mail message, it will be perfect but thanks for responding :) – Coldvibe Aug 29 '18 at 10:19