Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I make an upload with PHP for four image files and also do:
Change the names of all the files to PHP defined names

Only allow upload of .jpg and .png formats

Each time a form is submitted it should save the files to a different folder

Print out the root of each file with the format in a separate tag

Zip all the files that were uploaded and change the name of the zip file to "images:" and place the download link inside the tag

What I have tried:

This is the code that I made
PHP
<?php

 $Message_sent = false;
if(isset($_POST['email']) && $_POST['email'] != ' '){
 
    if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
 

$userName = $_POST['inputNmae'];
$userEmail = $_POST['inputEmail'];
$message = $_POST['inputMessage'];
$fileOne = $_POST['inputFileOne'];
$fileTwo = $_POST['inputFileTwo'];
$fileThree = $_POST['inputFileThree'];
$fileFour = $_POST['inputFileFour'];
$to = "my@email.com";
$body = "";


$body .=  "From: \r\n".$userName. "\r\n\r\n";
$body .= "Email: \r\n".$userEmail. "\r\n\r\n";
$body .= "Message: \r\n".$message. "\r\n\r\n";
$body .= "File 1: \r\n".$fileOne. "\r\n\r\n";
$body .= "File 2: \r\n".$fileTwo. "\r\n\r\n";
$body .= "File 3: \r\n".$fileThree. "\r\n\r\n";
$body .= "File 4: \r\n".$fileFour. "\r\n\r\n";



mail($to,$messageSubject,$body);

$Message_sent = true;
}


    
}

?>




<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <META HTTP-EQUIV=REFRESH CONTENT="5;URL=index.html">
  

  
</head>


<body>
<form action="mail.php" method="POST">

<label for="name">Name</label>
<input type="text" name="inputNmae" id="name">

<label for="email">Email</label>
<input type="email" name="inputEmail" id="email">

<label for="fileOne">Upload the summer photo</label>
<input type="file" name="inputFileOne" id="fileOne">
<p>your can only upload a file in the format of .jpg or .png</p>

<label for="fileTwo">Upload the fall photo</label>
<input type="file" name="inputFileTwo" id="fileTwo">
<p>your can only upload a file in the format of .jpg or .png</p>

<label for="fileThree">Upload the winter photo</label>
<input type="file" name="inputFileThree" id="fileThree">
<p>your can only upload a file in the format of .jpg or .png</p>

<label for="fileFour">Upload the spring photo</label>
<input type="file" name="inputFileFour" id="fileFour">
<p>your can only upload a file in the format of .jpg or .png</p>

<label for="message">Message</label>
<textarea name="inputMessage" id="message" cols="30" rows="10"></textarea>

</form>
    <?php
      if($Message_sent):
    ?>
      
      <div>
  <p>Your files have been uploaded to  <br>
    The first picture <span id="theFirstPictureRoot"></span>
    The second picture <span id="theSecondPictureRoot"></span>
    The third picture <span id="theThirdPictureRoot"></span>
    The fourth  picture <span id="theFourth PictureRoot"></span>
  </p> 
  <a href="" id="downloadZipFile">Download zip file</a>
</div>

    <?php
   else:
    ?>
    
    <?php
    endif;
    ?>
</body>
</html>
Posted
Comments
Member 15627495 13-Dec-22 12:51pm    
you divide the needs in a good way.
www.php.net


this part of php :
$userName = $_POST['inputName'];


could be :

$body .=  "From: \r\n".$_POST['inputName']."\r\n\r\n";



because $_POST is a var, an Array with all values in.
try it :
foreach($_POST as $item){
 echo $item;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900