Problem with multiple upload files

See all posts Reply

Problem with multiple upload files new!
by Geizon, 17 years, 2 months ago
Hello, I have the form code

when I go to send all archives results the following error
Warning: Invalid argument supplied will be foreach () in D:\Sites\administra _portal \ Galeria_Upload_Server.php on line 22

and if I leave one of the fields blank functions normal, as I arrange to send all? Code of line 222
$files = Array (); 
foreach ($_FILES [ââ?¬Å?my_fieldââ?¬Â] $k => $l)  { 
  foreach ($l $i => $v)  { 
    if (! array_key_exists ($i, $files)) 
      $files [$i] = Array (); 
      $files [$i] [$k] = $v; 
    }
  }
} 
Reply
Re: Problem with multiple upload files new!
by colin, 17 years, 2 months ago
First, you code should be:
$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; 
    }
  }
} 

Note the as in the foreach declarations.

Now, can you add this line before your foreach, and copy and paste here what it outputs?
die('FILES: '.print_r($_FILES, 1));
Reply
Re: Problem with multiple upload files new!
by Geizon, 17 years, 2 months ago
the output is
FILES: Array
(
    [my_field] => Array
        (
            [name] => Array
                (
                    [0] => gremio 001.jpg
                    [1] => gremio 002.jpg
                    [2] => gremio 003.jpg
                    [3] => gremio 004.jpg
                    [4] => gremio 005.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => image/jpeg
                    [3] => image/jpeg
                    [4] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => C:\WINDOWS\TEMP\phpE7.tmp
                    [1] => C:\WINDOWS\TEMP\phpE8.tmp
                    [2] => C:\WINDOWS\TEMP\phpE9.tmp
                    [3] => C:\WINDOWS\TEMP\phpEA.tmp
                    [4] => C:\WINDOWS\TEMP\phpEB.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                )

            [size] => Array
                (
                    [0] => 1785455
                    [1] => 1940836
                    [2] => 1512101
                    [3] => 1499155
                    [4] => 1279225
                )

        )

)
Reply
Re: Problem with multiple upload files new!
by colin, 17 years, 2 months ago
It looks all fine to me.

Does it still fail with smaller images?Reply
Re: Problem with multiple upload files new!
by Geizon, 17 years, 2 months ago
Yes, it showed this result, but when I go to send the 5 images continue the error. My friend also uses this class and had problem the same, for what I understood, if you to place 6 input file in the form, will only go to function to place photo only in 5, and so on, is the total but one pra to function

my code complete
include "../classes/MySql.class.php";
include "../classes/class.upload.php";
include "../funcoes/funcoes.php";

$db = new DB();
    
$pasta = protegeLinks($_POST["pasta"]);

// ---------- MULTIPLE UPLOADS ----------

// as it is multiple uploads, we will parse the $_FILES array to reorganize it into $files
$files = array();

foreach ($_FILES['my_field'] as $k => $l) {
    foreach ($l as $i => $v){
        die('FILES: '.print_r($_FILES, 1));
        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);
    $handle2 = 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 && $handle2->uploaded) {
        $data = $_POST["data"];
        $local = explode("-", $pasta);
        
        // configuratio para thumb
        $handle2->image_resize		= true;
        $handle2->image_ratio_y		= true;
        
        $handle2->image_x 			= 90;
        $handle2->jpeg_quality      = 100;

        $handle2->file_new_name_body   = ''.$local[0].'_'.$data.'';
        $handle2->file_new_name_ext = 'jpg';
        $handle2->Process('../galerias/'.$local[0].'/thumbs');
        
        
        // configuratio para imagem normal
        $handle->image_resize         = true;
        $handle->image_ratio_y        = true;
        $handle->image_x              = 400;
        
        $handle->jpeg_quality         = 90;
        
        $handle->image_watermark       = "../imagens/watermark.png"; 
        $handle->image_watermark_position = 'BR'; 

        $handle->file_new_name_body   = ''.$local[0].'_'.$data.'';
        $handle->file_new_name_ext = 'jpg';
        $handle->Process('../galerias/'.$local[0].'/normal');

        // we check if everything went OK
        if ($handle->processed && $handle2->processed)  {
            // insere no banco
            $ins_img = "INSERT INTO galeria_fotos (album_id, filename) VALUES ('".$local[1]."','".$handle->file_dst_name."')";
            $exe = $db->query($ins_img);
            
            // everything was fine !
            echo 'Arquivo enviado com sucesso';
            echo '  ';
        } 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
Re: Problem with multiple upload files new!
by colin, 17 years, 2 months ago
You need to instanciate the class only once into your loop.

Your code should be:
[...]
// 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) {
        $data = $_POST["data"];
        $local = explode("-", $pasta);
        
        // configuratio para thumb
        $handle->image_resize		= true;
        $handle->image_ratio_y		= true;
        
        $handle->image_x 			= 90;
        $handle->jpeg_quality      = 100;

        $handle->file_new_name_body   = ''.$local[0].'_'.$data.'';
        $handle->file_new_name_ext = 'jpg';
        $handle->Process('../galerias/'.$local[0].'/thumbs');
        
        
        // configuratio para imagem normal
        $handle->image_resize         = true;
        $handle->image_ratio_y        = true;
        $handle->image_x              = 400;
        
        $handle->jpeg_quality         = 90;
        
        $handle->image_watermark       = "../imagens/watermark.png"; 
        $handle->image_watermark_position = 'BR'; 

        $handle->file_new_name_body   = ''.$local[0].'_'.$data.'';
        $handle->file_new_name_ext = 'jpg';
        $handle->Process('../galerias/'.$local[0].'/normal');

        // we check if everything went OK
        if ($handle->processed)  {
           [...]
        } else  {
            [...]
        }
    } else {
        [...];
    }
}
Reply