【问题标题】:Multiple upload and resize class.upload.php多次上传和调整class.upload.php
【发布时间】:2013-09-13 04:28:10
【问题描述】:

我正在尝试将多张图片上传到服务器并为每张图片制作不同的分辨率版本。 为此,我第一次使用 class.upload.php。 http://www.verot.net/php_class_upload.htm

我查看文档并从演示示例http://www.verot.net/php_class_upload_download_zip.htm开始

我做了一个多输入的表格

<form name="form3" enctype="multipart/form-data" method="post" action="upload.php">
   <p><input type="file" size="32" name="my_field[]" value="" /></p>
   <p><input type="file" size="32" name="my_field[]" value="" /></p>
   <p><input type="file" size="32" name="my_field[]" value="" /></p>
   <p><input type="file" size="32" name="my_field[]" value="" /></p>
   <p><input type="file" size="32" name="my_field[]" value="" /></p>
   <p class="button"><input type="hidden" name="action" value="multiple" />
   <input type="submit" name="Submit" value="upload" /></p>
</form>

示例中的原始 php 只上传图片而不调整它们的大小:

$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
    foreach ($l as $i => $v) {
        if (!array_key_exists($i, $files))
            $files[$i] = array();
        $files[$i][$k] = $v;
    }
}

// now we can loop through $files, and feed each element to the class
foreach ($files as $file) {

    // we instanciate the class for each element of $file
    $handle = new Upload($file);

    // then we check if the file has been uploaded properly
    // in its *temporary* location in the server (often, it is /tmp)
    if ($handle->uploaded) {

        // now, we start the upload 'process'. That is, to copy the uploaded file
        // from its temporary location to the wanted location
        // It could be something like $handle->Process('/home/www/my_uploads/');
        $handle->Process($dir_dest);

        // we check if everything went OK
        if ($handle->processed) {
            // everything was fine !
            echo 'ok';
        } else {
            // one error occured
            echo '  Error: ' . $handle->error . '';
        }

    } else {
        // if we're here, the upload file failed for some reasons
        // i.e. the server didn't receive the file
        echo '  Error: ' . $handle->error . '';
    }
}

我想做的是处理 if ($handle->processed) {} 中的每个文件 因此,我采用了调整 img 大小的示例的函数并将其粘贴到 if ($handle->processed) {} 部分中。现在它看起来像这样:

if ($handle->uploaded) {

        // now, we start the upload 'process'. That is, to copy the uploaded file
        // from its temporary location to the wanted location
        // It could be something like $handle->Process('/home/www/my_uploads/');
        // now, we start a serie of processes, with different parameters
        // we use a little function TestProcess() to avoid repeting the same code too many times
        function TestProcess(&$handle, $title) {
            global $dir_pics, $dir_dest;

            $handle->Process($dir_dest);

            // we check if everything went OK
            if ($handle->processed) {
                // everything was fine !
                echo 'ok';
            } else {
                // one error occured
                echo '  Error: ' . $handle->error . '';
           }
        }
        if (!file_exists($dir_dest)) mkdir($dir_dest);

        // ----------- save the uploaded img adding _xl to the name
        $handle->file_name_body_add    = '_xl';
        $handle->file_overwrite = true;
        TestProcess($handle, 'File originale', '');

        // ----------- save the uploaded img adding _l to the name and downsizing it
        $handle->file_name_body_add    = '_l';
        $handle->image_resize          = true;
        $handle->image_ratio_y         = true;
        $handle->image_x               = 1024;
        $handle->file_overwrite = true;
        TestProcess($handle, 'Ridimensionato a 1024px');
    }

此时脚本仅适用于第一个 img。 它不会使“foreach ($files as $file)”进入 $files 数组... 你能帮我找出错误在哪里吗? 谢谢 丹尼尔

【问题讨论】:

    标签: php upload resize image-resizing


    【解决方案1】:

    好的,所以我尝试使用 verot 的答案,但我遇到了很多错误,我尝试了网络上的其他答案。

    这是可行的解决方案。

    $placeDir = $_SERVER['DOCUMENT_ROOT'] . '/myuploadfolder';
    
    $files = [];
    foreach ($_FILES['image_field']['name'] as $key => $value) {
        if(!empty($_FILES['image_field']['name'][$key])){
            $name  = $_FILES['image_field']['name'][$key];
            $type  = $_FILES['image_field']['type'][$key];
            $tmp   = $_FILES['image_field']['tmp_name'][$key];
            $error = $_FILES['image_field']['error'][$key];
            $size  = $_FILES['image_field']['size'][$key];
            $files[] = [
                  'name'     => $name
                , 'type'     => $type
                , 'tmp_name' => $tmp
                , 'error'    => $error
                , 'size'     => $size
            ];
        }
    }
    
    foreach($files as $file){
        $image = new uploadHelper($file);
        $image->allowed = array('image/*');
        if ($image->uploaded) {
          $image->process($placeDir);
          if ($image->processed) {
            echo 'image done';
            $image->clean();
          } else {
            echo 'error : ' . $image->error;
          }
        } else {
            echo '<h1>IMAGE NOT UPLOADED</H1>';
        }
    }
    

    我真的希望这可以帮助人们。

    【讨论】:

      【解决方案2】:

      这里的类的创建者...您需要先更改 $files 数组,如下所示。它在FAQ

      $files = array();
      foreach ($_FILES['my_field'] as $k => $l) {
       foreach ($l as $i => $v) {
       if (!array_key_exists($i, $files))
         $files[$i] = array();
         $files[$i][$k] = $v;
       }
      }    
      

      【讨论】:

      • 感谢您的回答。我制作了一个包含 10 个输入的表单来加载多个图像现在我可以上传和调整 2 个图像的大小,但是如果我在表单中插入第三个图像,这将被上传但没有调整大小并且在处理第三个图像之后没有图像......我真的不不明白为什么!在这里你可以找到我从文档danielepennati.com/prove/upload_php/index.html开始制作的脚本
      • 似乎对我不起作用...我遇到了很多错误,并且在尝试使用此类时遇到了很多问题。感觉自己因为我花了这么多时间而自欺欺人……老板不高兴注意:未定义的索引:第 2361 行 /home/webstarters/admin/public_html/backend/components/helpers/uploadHelper.php 中的 tmp_name 通知: 未定义的索引:/home/webstarters/admin/public_html/backend/components/helpers/uploadHelper.php 中的大小 2379 行 注意:未定义的索引:输入 /home/webstarters/admin/public_html/backend/components/helpers/uploadHelper .php 在第 2380 行。
      • 我需要能够使用 image_field[],我不明白如何在不处理图像数组的情况下创建这么大的类 -.- 当我找到解决方案时,我们能否以某种方式在类中实现它所以其他人不会遇到这些问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 2015-09-13
      • 2016-03-22
      • 2017-08-20
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      相关资源
      最近更新 更多