Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Image Extension

  • 18-01-2012 10:06PM
    #1
    Registered Users, Registered Users 2 Posts: 7,994 ✭✭✭


    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, Registered Users 2 Posts: 7,994 ✭✭✭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