SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,932
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Which File to "unlink"?

    From another discussion...


    Quote Originally Posted by ScallioXTX View Post
    Yes, with unlink. Just delete the uploaded file once you're done with it (stored, resized it, etc).
    Without posting my entire script here, which file(s) should I be deleting?

    Towards the top of my script I have...
    PHP Code:
        if (empty($errors)){
            
    // Valid Form Data.
            
    if (empty($_FILES['userPhoto']['tmp_name'])){
                
    // No File.
                
    $errors['upload'] = 'Choose a File.';

            }else{
                
    // File exists.
                
    $tempFile $_FILES['userPhoto']['tmp_name'];
            }

        }else{
            
    // Invalid Form Data.
            // Drop through to display Errors.

        
    }//End of CHECK FOR FILE 

    In a lot of my "upload.php" script I use $tempFile to check if the uploaded photo is "clean".

    But then once I start working with GD, I am working a lot with functions which create an "Image Resource Identifier".

    So what about these snippets of code...

    PHP Code:
        $origImage = @imagecreatefromgif($tempFile); 
    PHP Code:
        $newTrueColorImage = @imagecreatetruecolor($newWidth$newHeight); 
    PHP Code:
        $resizedImage = @imagecopyresampled($newTrueColorImage$origImage0000$newWidth$newHeight$origWidth$origHeight); 
    PHP Code:
        $newPhoto = @imagegif($newTrueColorImage$newFilePath); 

    Follow me??

    Based on my understanding of things, it seems like I need to delete either $_FILES or $_FILES['userPhoto']['tmp_name'] at the very end of my script like this...

    PHP Code:
    unlink($_FILES
    or

    PHP Code:
    unlink($_FILES['userPhoto']['tmp_name']) 

    But what about the intermediary "Image Resource Identifiers"??

    And maybe there are some other files I am missing??

    Sincerely,


    Debbie

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As far as I can tell, all bar $newPhoto are memory-resident and are trashed when garbage collection runs. And even $newPhoto is really only a handle/reference to the new photo - the actual file is physycally saved at $newFilepath location.

    So to answer your question (I think), you only need to unlink the the temp file.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    $_FILES is an array. You can't unlink an array as that doesn't make any sense. So yes, you would need to delete the temp file.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,932
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    $_FILES is an array. You can't unlink an array as that doesn't make any sense. So yes, you would need to delete the temp file.

    Based on the "unlink" in the PHP Manual, I'm a little unsure of what to do as far as error-handling?!


    Is this code sufficient...

    PHP Code:
        switch ($imageType){
            case 
    IMAGETYPE_GIF:
                
    $newPhoto = @imagegif($newTrueColorImage$newFilePath);
                break;

            case 
    IMAGETYPE_JPEG:
                
    $newPhoto = @imagejpeg($newTrueColorImage$newFilePath);
                break;

            case 
    IMAGETYPE_PNG:
                
    $newPhoto = @imagepng($newTrueColorImage$newFilePath);
                break;

            default:
                
    $newPhoto FALSE;
        }

    //NEW
        // Delete Temporary File.
        
    unlink($_FILES['userPhoto']['tmp_name']); 
    Sincerely,


    Debbie

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,932
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Yes
    Okay, thanks for the help, everyone!!


    Debbie


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •