Jump to content

Recommended Posts

Hi everyone - thanks for taking the time to read this. I have a form with 35 file input boxes as part of a CMS for my customer to upload 35 pictures of each his products.  The breakdown of that is 7 pictures of the black version, 7 pictures of the blue, 7 pictures of the grey, 7 pictures of the red, and 7 pictures of the white version of each product.  So that's 35 total pictures he needs to upload.  Additionally, for each of those files that he uploads, a smaller "thumbnail" sized picture needs to be made.  I have a file upload script that I always use that works beautifully - when there's one file to upload.  I'm not sure how to apply it in this case for 35 files.  Each input box has a unique name (black1, black2...black7, blue1, blue2...blue7, etc) so, technically I could repeat the upload code 35 times with the unique name of each file input box to do this, but that is obvoiusly extremely inefficient.  I'm hoping someone here can help me out with a better solution. 

 

An additional requirement is that the names of the files be stored in a database.  All of the filenames of the black pictures should be put into a string, separated by commas, and stored in the `blackpics` column of the database.  All of the filenames of the blue pictures should be put into a string, separated by commas, and stored in the `bluepics` columns of the database.  And so on for the grey, red, and white pictures. 

 

Here is the code that I have now to upload one file.  It gets the file from input box "file", checks that it's of the right extension (an image file), checks the filesize, creates a random file name with a random number and timestamp,  creates a thumbnail (448px x 298px - big thumbnail, I know), checks that the original image uploaded was of the right dimensions (873px x  581px), and if everything is okay, I end up with the big file saved in ../images/store/big/  and the thumb saved in ../images/store/small/.  They both have the same filename, they're just stored in different directories. Temporary files are deleted and all that, and if there are any errors, the files are deleted.  As I said, this works great for one file. 

 

So what I need to do is modify the code so that it does all of this for input box "black1", "black2"..."black7", then saves all the filenames into a string (black1.jpg,black2.jpg,black3.jpg,black4.jpg,black5.jpg,black6.jpg,black7.jpg) which I can then store in the 'blackpics' column of the database. Same for the blue, grey, red, and white.  I don't need any help with the database part.  I'm thinking that I need to create a function containing the file upload script that returns the filename.  Then call that function 35 times, one for each of the input boxes.  But I could be wrong. 

 

If anyone could offer me any assistance, I would greatly appreciate it.  Here is the code for the upload script:

<?php

 $filename = $_FILES["file"]["name"];
 $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
 $file_ext = substr($filename, strripos($filename, '.')); // get file name
 $filesize = $_FILES["file"]["size"];
 $allowed_file_types = array('.jpg','.gif','.png', '.JPG');
   
   if (in_array($file_ext,$allowed_file_types)  &&  ($filesize < 1024000)) {
      // rename file
        $rand = rand(1,100000000);
        $time = time();
        $newfilename = $rand . $time . $file_ext;
            if (file_exists("../images/store/big/" . $newfilename)) {        
            // file already exists error
                $err[] =  "You have already uploaded this file.";            
            } else {        
                move_uploaded_file($_FILES["file"]["tmp_name"], "../images/store/big/" . $newfilename);
                $pathToImage = '../images/store/big/' . $newfilename;
                $pathToThumb = '../images/store/small/' . $newfilename;
                $last4 = substr($pathToImage, -4);

                switch(strtolower($last4)) {
                  case '.jpeg':
                    $img = imagecreatefromjpeg($pathToImage);
                    break;
                  case '.jpg':
                    $img = imagecreatefromjpeg($pathToImage);
                    break;
                  case '.png':
                    $img = imagecreatefrompng($pathToImage);
                    break;
                  case '.gif':
                    $img = imagecreatefromgif($pathToImage);
                    break;
                  default:
                    exit('Unsupported type: '. $pathToImage);
                }
                
                $max_width = 448;
                $max_height = 298;

                // Get current dimensions
                $old_width  = imagesx($img);
                $old_height = imagesy($img);

                // Calculate the scaling we need to do to fit the image inside our frame
                $scale = min($max_width/$old_width, $max_height/$old_height);

                // Get the new dimensions
                $new_width  = ceil($scale*$old_width);
                $new_height = ceil($scale*$old_height);
                
                $tmp_img = imagecreatetruecolor($new_width, $new_height);
                imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
    
                switch(strtolower($last4)) {
                  case '.jpeg':
                    imagejpeg($tmp_img, $pathToThumb);
                    break;
                case '.jpg':
                    imagejpeg($tmp_img, $pathToThumb);
                    break;
                case '.png':
                    imagepng($tmp_img, $pathToThumb);
                    break;
                case '.gif':
                    imagegif($tmp_img, $pathToThumb);
                    break;
                default:
                    exit('Unsupported type: '. $pathToImage);
                }
                imagedestroy($tmp_img);
                imagedestroy($img);
              }
           } elseif (empty($file_basename)) {    
            $err[] =  "Select a file to upload";
        } elseif ($filesize > 1024000) {    
            $err[] =  "File size limit exceeded";
        } else {    
            $err[] =  "File type not allowed";
            unlink($_FILES["file"]["tmp_name"]);
        }

        list($width, $height) = getimagesize("../images/store/big/$newfilename");
        if ($width != "873" || $height != "581") {
           $err[] = "File dimensions error";
           unlink("../images/store/big/$newfilename");
           unlink("../images/store/small/$newfilename");
         }
 ?>

And in the body I have the file upload fields as so...

<input type="file" name="black1" disabled="1">
<input type="file" name="black2" disabled="1">
...
<input type="file" name="black7" disabled="1">

<input type="file" name="blue1" disabled="1">
<input type="file" name="blue2" disabled="1">
...
<input type="file" name="blue7" disabled="1">

and so on for grey, red, and white.

 

Like I said, if anyone can help me out, I would greatly appreciate it.  And if you made it all the way down here, thanks again for taking the time to read all of this. ::)

Edited by halosinfire
Link to comment
https://forums.phpfreaks.com/topic/277687-file-upload-script-for-35-files/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.