dq1685513999 2013-09-09 16:02
浏览 22
已采纳

多次上传并调整class.upload.php的大小

I'm trying to upload multiple image to the server and to make different resolution version of each image. To do this I'm using class.upload.php for the first time. http://www.verot.net/php_class_upload.htm

I look at the documentation and starting from the demo example http://www.verot.net/php_class_upload_download_zip.htm

I made a form with multiple input

<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>

the original php from the example only upload the image without resizing them:

$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 . '';
    }
}

What I'd like to do is to process each file inside the if ($handle->processed) {} so I took the function form the example that resize img and paste it inside the if ($handle->processed) {} part. Now it look like this:

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');
    }

At this point the script works fine only with the first img. it not make the "foreach ($files as $file)" trow the $files array... could you help my find where the error is? thaks Daniele

  • 写回答

1条回答 默认 最新

  • dpstir0081 2013-10-08 16:16
    关注

    Creator of the class here... You need to change the $files array first, as following. It is in the 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;
     }
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算