2013-09-09 5 views
1

Я пытаюсь загрузить несколько изображений на сервер и сделать другую версию разрешения для каждого изображения. Для этого я использую class.upload.php в первый раз. http://www.verot.net/php_class_upload.htmМножественная загрузка и изменение размера class.upload.php

Я смотрю на документации и начиная с демонстрационным примером 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-> обработан) {} , поэтому я взял функцию в примере, который изменяет размер img и вставляет его внутри части if ($ handle-> processing) {}. Теперь это выглядит так:

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 ... вы могли бы помочь мне найти, где ошибка? thaks Daniele

ответ

5

Создатель класса здесь ... Сначала необходимо изменить массив $ 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; 
} 
}  
+0

Благодарим за ответ. Я сделал форму с 10 входами для загрузки нескольких изображений Теперь я могу загрузить и изменить размер 2 изображений, но если я вставлю третий в форму, это будет загружено, но не будет изменено, а изображение после третьего будет обработано ... I действительно не понимаю, почему! здесь вы можете найти сценарий, который я сделал, начиная с документации http://www.danielepennati.com/prove/upload_php/index.html – danipen