Multiple upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Multiple upload

Post by MiniMonty »

Hi all,

I've got an upload script which works nicely - uploads an image, prints a success
message and a thumbnail of the image you just uploaded.

I'd like to "convert" this script to handle multiple uploads (5 images at once) and am
unsure how to go about it so all and any advice very much appreciated.
(my original upload code below)

Best wishes
Monty

Code: Select all

 
if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 0)){
    $image = $_FILES['fileField']['name'];
    $filename = stripslashes($_FILES['fileField']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);
   
        //Filetype check
    if (!in_array($extension,$acceptableextensions)){
        exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
        echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
        $errors=1;
       
        //Filesize check
    } else if ($_FILES['fileField']['size'] > $maxfilesize ) {
        $error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
        unlink($_FILES['fileField']['tmp_name']);
    } else {
            // rename the picture incrementally
        $extension = getExtension($filename);
        $extension = strtolower($extension);
        $groovy = sizeof(glob("members/$id/images/*"));
        $groovy = ++$groovy;
 
        $image_name=$groovy.'.'.$extension;
        $newname="".$image_name;
        $gowhere = mysql_real_escape_string($_POST['gowhere']);
        
            // NOW make the resize call !
        //img_resize ($_FILES ['fileField'] [ 'name'], $_FILES [ 'fileField'] [ 'tmp_name'], 25, $newname);
 
 
            // INSERT image into the DB
        $sql = mysql_query("INSERT INTO pictures
        (UID,filename,dirpath,gallery,dtadded)
        VALUES ('$id','$newname','/members/$id/images/$newname','$gowhere',now())")
        or die (mysql_error());
                       
        $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
        chmod ("members/$id/images/$newname", 0644);
        //////////  MY RESIZE EFFORT !!
              //$imagepath = $newname;
              $save = "members/$id/images/" .$newname; //new file to save
              $file = "members/$id/images/" .$newname; //original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 750; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 100) ; 
 
              $save = "members/$id/images/sml_" . $newname; //This is the new file you saving
              $file = "members/$id/images/" . $newname; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 80; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 100) ; 
           
            $thumbToPrint = "<img src='members/$id/images/sml_".$newname."'>";
        
        
        
        
        
            //REDUNDANT NOW but keep for reference...  NOW WRITE THE  .txt FILE IN THE APPROPRIATE GALLERY FOLDER
        //$myFile = "images/gallery/".$_POST ['gowhere']. "/gallarray.txt";
        //$fh = fopen($myFile, 'a+') or die("can't open file");
        //$stringData = "members/".$id."/images/".$newname .",";
        //fwrite($fh, $stringData);
        //fclose($fh);
        }
       
        $success_msg = '<span class="Big_Orange_Times">Your image has been uploaded. it may take a few moments to appear in your gallery, please be patient.<br>';
 
         
        } else if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 4)){ 
        //Error code 4: No image uploaded
    $error_msg = 'Please browse for an image before you press Go.';
    
}
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multiple upload

Post by AbraCadaver »

Should be easy. Just setup your form fields as an array like this:

Code: Select all

<input type="file" name="fileField[]" />
And then loop through your existing code:

Code: Select all

foreach($_FILES['fileField'] as $file) {
 
    // your code, change $_FILES['fileField'] var to $file
 
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Multiple upload

Post by Eran »

Unfortunately an array of file inputs works in a somewhat backwards way and not in the manner abracadaver suggested (which would have been great) - each inner attribute of the $_FILES superglobal will become an array. So you would need to iterate one of the inner properties (such as 'name', 'tmp_name' etc) to go through all the files.

Code: Select all

foreach($_FILES['fileField']['tmp_name'] as $tmpName) {
    // Handle file
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multiple upload

Post by AbraCadaver »

pytrin wrote:Unfortunately an array of file inputs works in a somewhat backwards way and not in the manner abracadaver suggested (which would have been great) - each inner attribute of the $_FILES superglobal will become an array. So you would need to iterate one of the inner properties (such as 'name', 'tmp_name' etc) to go through all the files.

Code: Select all

foreach($_FILES['fileField']['tmp_name'] as $tmpName) {
    // Handle file
}
Well that sucks! Thanks for the correction.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Multiple upload

Post by AbraCadaver »

I guess you'd want the key as well to make it easier:

Code: Select all

foreach($_FILES['fileField']['tmp_name'] as $key => $tmpName) {
    $name = $_FILES['fileField']['name'][$key];
    $size = $_FILES['fileField']['size'][$key];
    //etc
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply