dpmrakfbx820320638 2013-05-06 07:58
浏览 22
已采纳

以一种形式上传35个文件的文件

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.

  • 写回答

1条回答 默认 最新

  • dongzhuandian3292 2013-05-06 08:12
    关注

    First don't use dimensions for images. Dimensions do not say much about the size of the image. And the size matters for displaying the image on a website, not the dimensions.

    Second why not use a multipart uploading form? See here. And then your client could select the images colourwise and upload them with one selection, which would reduce the clicks from 35 to seven. Or if you trust your client to be more tech-savvy: Use only one input field and instruct him to name his files in a specific way. Like "b_[name of file].[extension]" for a black image. Then use your favourite string searching method - for example RegEx - to identify the images classes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本