Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Image Extension

Options
  • 18-01-2012 10:06pm
    #1
    Registered Users Posts: 8,004 ✭✭✭


    Hi Folks,

    Have an odd script problem with PHP.

    I'm using this script here as it allows me to resize etc really easily

    Link: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

    The problem is that I can't seem to get the name of the image or the extension. Here is my code:
    //New Upload
    
    include('SimpleImage.php');
          $image = new SimpleImage(); //Create class
          $image->load($_FILES['logo']['tmp_name']); //Load the file
          $image->resizeToWidth(285); //Resize
          //$image->output(); //Debug output
    $image->save('uploads/picture2.jpg');
    

    This works fine. However if I try the following:
    //Get file extension
    $fullfile =  $_FILES["logo"]["tmp_name"]; //Try to get the full file name
    
    //Or try this
    
    $fullfile =  basename($_FILES["logo"]["tmp_name"]); //Doesn't work either
    echo $fullfile; 
    

    Also want I really want to do is make each picture named differently with a salt so no two pictures have the same name. Hence why I need the extension, so that I can store it in the database

    
    $saveString = md5($_FILES['logo'].time()); //Use time as a salt
    echo $saveString; //Works fine
    
    //Try to add the extension
    $saveString = $saveString."."$_FILES['tmp_name']; //No joy doing this
    //Add $saveString to DB
    

    I should note, in a simpler script the upload works fine. It just seems to be getting the extension is a problem.

    Any pointers on any of the above?


Comments

  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    The tmp_name is just that - a temp name, often ending in .tmp

    You need to get the actual file name & extension from the original form variable used to post the file

    Also there's a concatenation dot missing after the ."." in the last line of code.


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Liam Byrne wrote: »
    The tmp_name is just that - a temp name, often ending in .tmp

    You need to get the actual file name & extension from the original form variable used to post the file

    Cheers for that. I solved it earlier today and that was the issue.
    
    //New Upload Method
    
    $imageName = $_FILES["logo"]["name"];
    //echo $imageName;
    
    $extension = explode('.',$imageName); //Get extension i.e. After the dot
    //echo $extension[1];
    
          include('SimpleImage.php');
          $image = new SimpleImage();
          $image->load($_FILES['logo']['tmp_name']);
          $image->resizeToWidth(285);
          //$image->output();
    
    $saveString = md5($imageName.time()); //A random unique name
    //echo $saveString;
    
    $final = $saveString.".".$extension[1]; //The image to save plus extension
    echo $final;
    $final = "uploads/".$saveString.".".$extension[1]; //Add the path
    $image->save($final); //Save
    
    //End New
    


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    ironclaw wrote: »
    Liam Byrne wrote: »
    The tmp_name is just that - a temp name, often ending in .tmp

    You need to get the actual file name & extension from the original form variable used to post the file

    Cheers for that. I solved it earlier today and that was the issue.
    //New Upload Method
    
    $imageName = $_FILES["logo"]["name"];
    //echo $imageName;
    
    $extension = explode('.',$imageName); //Get extension i.e. After the dot
    //echo $extension[1];
    
          include('SimpleImage.php');
          $image = new SimpleImage();
          $image->load($_FILES['logo']['tmp_name']);
          $image->resizeToWidth(285);
          //$image->output();
    
    $saveString = md5($imageName.time()); //A random unique name
    //echo $saveString;
    
    $final = $saveString.".".$extension[1]; //The image to save plus extension
    echo $final;
    $final = "uploads/".$saveString.".".$extension[1]; //Add the path
    $image->save($final); //Save
    
    //End New
    
    No bother.

    If it's an open form I'd put in a few extra checks to ensure someone doesn't upload something malicious.


Advertisement