Problem with multiple upload files

See all posts See thread Reply

Re: Problem with multiple upload files new!
by Geizon, 17 years, 3 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, 3 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