Reply to Re: How to process multiple images that are inside a folder?

Re: How to process multiple images that are inside a folder? new!
by Marcio Alves, 12 years, 6 months ago
Hi Colin. Thanks for help me.

First I am listing the files that are inside the folder with the following code:

$diretorio = '/home/ceanorg/public_html/fotos/galeria/teste/';

// abre o diretório
$ponteiro  = opendir($diretorio);

// monta os vetores com os itens encontrados na pasta
while ($nome_itens = readdir($ponteiro)) {
    $itens[] = $nome_itens;
}

// ordena o vetor de itens
sort($itens);

// percorre o vetor para fazer a separacao entre arquivos e pastas 
foreach ($itens as $k) {

  // retira "./" e "../" para que retorne apenas pastas e arquivos
  if ($k!="." && $k!=".."){
    
    // checa se o tipo de arquivo encontrado é uma pasta
    if (is_dir($k)) {

      // caso VERDADEIRO adiciona o item à variável de pastas
      $pastas[]=$k;
  
    } else {
      
      // caso FALSO adiciona o item à variável de arquivos
      $files[]=$k;
    }
  }
}

So far, so OK

Then I want to put the files listed in an array. Exactly where the error happens.

1) If I use this (manual), the class works perfectly:
$files = array('/home/ceanorg/public_html/fotos/galeria/teste/001.jpg', 
  '/home/ceanorg/public_html/fotos/galeria/teste/002.jpg', 
  '/home/ceanorg/public_html/fotos/galeria/teste/003.jpg');


2) If I use this other way, the class is not executed
$files = array();


3) The remainder of the code is this:

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

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

  // 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->image_resize            = true;
    $handle->image_ratio_y           = true;
    $handle->image_x                 = 100;
    $handle->Process('/home/ceanorg/public_html/fotos/galeria/teste/');
    
    // we check if everything went OK
    if ($handle->processed) {
        // everything was fine !
        echo '  file uploaded with success';
        echo '  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
        echo '  link to the file just uploaded: ../../fotos/galeria/teste/' . $handle->file_dst_name . ' : ' . $handle->file_dst_name . '';
    } else {
        // one error occured
        echo '  file not uploaded to the wanted location';
        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 '  file not uploaded on the server';
    echo '  Error: ' . $handle->error . '';
  }
}
Reply

Your reply

Name *
Email 
Title *
Text *
CAPTCHA image
Enter the code displayed on the image:
Click on the image to generate another one if it is hard to read it. The case is important