Page 1 of 1

Multiple upload

Posted: Sat Jan 30, 2010 5:13 pm
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.';
    
}
 

Re: Multiple upload

Posted: Sat Jan 30, 2010 5:31 pm
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
 
}

Re: Multiple upload

Posted: Sat Jan 30, 2010 5:42 pm
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
}

Re: Multiple upload

Posted: Sat Jan 30, 2010 7:01 pm
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.

Re: Multiple upload

Posted: Sat Jan 30, 2010 7:05 pm
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
}