Jump to content

uploading multiple files at once


beemer832

Recommended Posts

I need to be able to upload multiple files with the use of one form. Right now I have support for one file and it works great. I am stuck on what route I should take for times sake and reliability and functionality. Can I run each file on its own through the PHP script to upload the file; I would have to create a loop to run through the script as many times as there are files. OR Do I create new functionality and add the files through the use of an array?

 

This is where I am getting the ARRAY idea: http://www.phpeasystep.com/phptu/2.html

 

This is the PHP code that is submitting the image and uploading to file system. This is what I would use to loop through multiple files if I take the loop method.

 

<?
header("location: /classifieds/index.php");

echo '<html><center>';

//first lets upload any files that were selected//
$date = date("m/d/y",time());

//check 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"] < 2500000)) {
    
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/uploads/'.$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))) {
            //strip off file path for $newname variable so path is not accessible via html//
            $tempnewname = explode('/', $newname);
            echo $tempnewname;
            $newname=$tempnewname[9].'/'.$tempnewname[10];
           echo "It's done! The file has been saved as: ".$filename;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         //echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";//
         $timestampname = str_replace('.jpg', date('j-n-Y_g:i:s').'.jpg', (basename($_FILES['uploaded_file']['name'])));
         $path = dirname(__FILE__).'/uploads/';
         $fullname = $path.$timestampname;
         
            
            //strip off file path for $newname variable so path is not accessible via html//
            $tempnewname = explode('/', $fullname);
            $newname=$tempnewname[7].'/'.$tempnewname[8];
            $picname=$tempnewname[8];
            
            
         ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)));
      }
    } else {
        echo "Error: Only .jpg images under 2.5MB are accepted for upload";
           }
}

else {
echo "Error: No file uploaded";
}
?>

 

Thanks for hte help

-Beemer

Link to comment
https://forums.phpfreaks.com/topic/217409-uploading-multiple-files-at-once/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.