multiple images upload (i have the script for 1 image upload)

Discussion in 'PHP' started by Kyriakos, Jun 12, 2009.

  1. #1
    hi,

    i have an PHP script that i can upload 1 image but i want to upload 5 images together in my server. this is my PHP script:
    <?php if ($_POST["uplfile"] == "1") {
    
    //Сheck that we have a file
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
      //Check if the file is JPEG image and it's size is less than 350Kb
      $filename = basename($_FILES['uploaded_file']['name']);
      $ext = substr($filename, strrpos($filename, '.') + 1);
      if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
        ($_FILES["uploaded_file"]["size"] < 500001)) {
        //Determine the path to which we want to save this file
          $newname = dirname(__FILE__).'/upload/'.$filename;
          //Check if the file with the same name is already exists on the server
          if (!file_exists($newname)) {
            //Attempt to move the uploaded file to it's new place
            if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
               echo "It's done! The file has been saved as: ".$newname;
            } else {
               echo "Error: A problem occurred during file upload!";
            }
          } else {
             echo "The file ".$_FILES["uploaded_file"]["name"]." already exists.";
          }
      } else {
         echo "Only files up to 500kb can upload";
      }
    } else {
     echo "Error: you doesn't have upload any file";
    }
    ?>
    PHP:
    this is my form:
    <form enctype="multipart/form-data" action="upload.php" method="post" style="margin:0px;">
    <input type="hidden" name="MAX_FILE_SIZE" value="500000"/><input type="hidden" name="uplfile" value="1"/>
    <input name="uploaded_file" type="file"/>
    <input type="submit" class="submit" value="Upload" />
    </form>
    Code (markup):
    thanks in advance
     
    Kyriakos, Jun 12, 2009 IP
  2. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You can change your input to
    <input name="uploaded_file[]" type="file"/>
    Code (markup):
    For example, for 5 boxes try this:
    
    <?php for($i=0;$i<5;$i++){ ?>
    <input name="uploaded_file[]" type="file"/>
    <?php } ?>
    PHP:
    Should give you 5 upload boxes. Now, $_POST['uploaded_file'] will now be an array because the brackets ([]). You can run the same script now, just replace proper vars and put it in a foreach().

    
    foreach($_POST['uploaded_file'] as $key => $key2){
      foreach($key2 as $val){
        // Upload script...
      }
    }
    PHP:
    Is that what you need? Or what else is the problem?

    Regards,
    Dennis M.
     
    Dennis M., Jun 12, 2009 IP