Jump to content

Delete file


drisate

Recommended Posts

Hey guys!

Everytime i upload something to the server theres a files created called Resource id #10 (number increments after eachj upload)

From what i can ubserve, those are the TMP file ... but for somereason it's staying there and piles up and takes out a lot of space.

 

Is there a way to delete them when the upload is finished?

Link to comment
Share on other sites

Here's my code

 

function photo($image, $largeur_maximum, $destination){
    $autorises = 'gifjpegjpgpngGIFJPEGJPGPNG';
    $extension = explode('.', $destination);
    $extension = strtolower($extension[sizeof($extension) - 1]);

    if (ereg($extension, $autorises)) {
        $dimensions = getimagesize($image);
        $largeur_actuelle = $dimensions[0];
        $hauteur_actuelle = $dimensions[1];

        if ($largeur_maximum == "original") {
            $nouvelle_largeur = $dimensions[0];
            $nouvelle_hauteur = $dimensions[1];
        } else {
            if ($largeur_actuelle > $largeur_maximum) {
                $nouvelle_largeur = $largeur_maximum;
                $nouvelle_hauteur = $hauteur_actuelle / ($largeur_actuelle / $largeur_maximum);
            } else {
                $nouvelle_largeur = $dimensions[0];
                $nouvelle_hauteur = $dimensions[1];
            }
        }

        if ($extension == 'jpg' || $extension == 'jpeg') {
            $image_p = imagecreatetruecolor($nouvelle_largeur, $nouvelle_hauteur);
            $image = imagecreatefromjpeg($image);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $nouvelle_largeur, $nouvelle_hauteur,
                $largeur_actuelle, $hauteur_actuelle);
            imagejpeg($image_p, $image);
        } else
            if ($extension == 'gif') {
                $image_p = imagecreatetruecolor($nouvelle_largeur, $nouvelle_hauteur);
                $image = imagecreatefromgif($image);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $nouvelle_largeur, $nouvelle_hauteur,
                    $largeur_actuelle, $hauteur_actuelle);
                imagegif($image_p, $image);
            } else
                if ($extension == 'png') {
                    $wm = imagecreatefrompng($image);

                    //Position du WM sur l'image finale
                    $positionx = 0;
                    $positiony = 0;

                    //Calcul de l'image du watermark
                    $largeurW = imagesx($wm);
                    $hauteurW = imagesy($wm);

                    //Si l'image source est plus large que celle du WM, on effectue la mise en place du WM
                    if ($largeurSrc > $largeurW) {
                        # Création de l'image finale aux dimensions de l'image qui va accueillir le WM
                        $final = imagecreatetruecolor($largeurSrc, $hauteurSrc);

                        # configuration du canal alpha pour le WM
                        imagealphablending($wm, false);
                        imagesavealpha($wm, true);

                        # Ajout du watermark
                        imagecopyresampled($final, $wm, $positionx, $positiony, 0, 0, $largeurW, $hauteurW,
                            $largeurW, $hauteurW);
                        # Pour ajouter d'autre photo par dessus, procéder de la meme façon

                        # affichage de l'image finale
                        header("Content-type: image/png");
                        imagepng($image);
                    }
                }
        copy($image, $destination);
    }
}

 

when i wana upload a file i simply do this:

 

                    $rand = rand("1000", "90000") . "-";
                    $name = $rand . nomValide($_FILES['image']['name']);
                    photo($_FILES['image']['tmp_name'],$media_small_whidth,'../'.$media_small.$name);
                    photo($_FILES['image']['tmp_name'],$media_medium_whidth,'../'.$media_medium.$name);
                    photo($_FILES['image']['tmp_name'],$media_medbig_whidth,'../'.$media_medbig.$name);
                    photo($_FILES['image']['tmp_name'],$media_big_whidth,'../'.$media_big.$name);
                    photo($_FILES['image']['tmp_name'],'original','../'.$media_original.$name);

 

This creates 4 version of the same photo with diffrent width + the original format. All versions are used in the site depending of the page.

 

Link to comment
Share on other sites

Your code is reusing the $image variable to hold the GD image resource, so when you use $image later in the code as the name for the file, you get files named like Resource id #10...

 

I suspect that the code wanted to use the $destination variable as the second parameter in the imagejpeg(), imagegif(), imagepng() statements so that the resulting images would be stored directly in the destination.

 

 

Link to comment
Share on other sites

What if i do something like this at the end

 

function delete_photo_tmp (){
$myDirectory = opendir(".");
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
closedir($myDirectory);
$indexCount	= count($dirArray);
sort($dirArray);
for($index=0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
if ( strstr($dirArray[$index], "Ressource id #") ) {
unlink("$dirArray[$index]");
}
}
}
}

Link to comment
Share on other sites

Why don't you just directly store the images at the final destination as has been suggested?

 

What you are doing now won't work when you have multiple concurrent visitors because the intermediate output filenames are not unique and files can get overwritten before you copy them to the final destination.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.