views:

98

answers:

1

I'm trying to play with image resizer and I got the following code

if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])){
     $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);
     move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);
     ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/" . $targetfilename, $width, $height);
    }

For now, the original image and the thumbnail will place in same folder.

Let me know..

Source

+2  A: 

Well, the answer is :

if (is_uploaded_file(@$_FILES['ulimage']['tmp_name']))
{
        $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);
        move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);
        ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height);
}

But maybe you'd like to understand a bit the code you copy and past from the Net before using it. Using $_ vars without escaping system and with @ to hide error is not really calling for trust...

EDIT : I'm giving advices, but maybe it's better to give some explanation as well.

// first you check if the is done uploading in the tmp directory with is tmp name
if (is_uploaded_file(@$_FILES['ulimage']['tmp_name'])) 
{
     // here, you rebuild a explicit name using the original filename and a 
     // unique ID to avoid erasing another one   
     $targetfilename = ImageHelper::treatFilename(uniqid() . "_" . $_FILES['ulimage']['name']);

     // you rename the file an put it in ./tmp, a subdir of the 
     // script file (because of dirname(__FILE__))
     move_uploaded_file($_FILES['ulimage']['tmp_name'], dirname(__FILE__) . "/tmp/" . $_FILES['ulimage']['name']);

    // Here create a rezided copy
    // so it's here you can decide to make it go to ./tmp/thumb
    // make sure the dir exists before because you have no clue here
    // if ImageHelper will create it for you if not
    ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/thumb/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height);
}
e-satis
i tried.. thumb not created into "/tmp/thumb/" only original size generated.
bob
As I wrote, "make sure the dir exists before because you have no clue here if ImageHelper will create it for you if not'. You can to that with mkdir : http://fr.php.net/manual/fr/function.mkdir.php
e-satis
thanks for your explanation to the code.. thanks.. yes folder i created (777) but thumbnail not coming too.
bob
Did yo manually created the folder of did you programmatically did it. According to the code, the file is not created in /tmp/thumb but in /path/to/script/tmp/thumb/.
e-satis
manually.. if I changed all the path to /tmp/thumb/ image will working both, original and thumb.
bob
ok I got. I put the tmp here. @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height);
bob
Actually I made a typo and put the /thumb in the wrong place. Shame on me :-)
e-satis
np, your inputs help me so much and working code ImageHelper::resizeImage(dirname(__FILE__) . "/tmp/" . @$_FILES['ulimage']['name'], dirname(__FILE__) . "/tmp/thumb/" . $targetfilename, $width, $height); if we set the thumb on first
bob