Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Removing characters from an uploaded file

    I have a system whereby a user can upload a picture onto my server, I have this check in place to make sure they must upload a .jpg

    PHP Code:
          $i=@getimagesize($_FILES['image_file']['tmp_name']);
            if (
    $i[2]==2) {
                
    $errortype 0;
                } else {
                
    $errortype 1;
            } 
    My problem is that, if the filename contains a ' the whole system gets messed up, the apostrophe is causing the problem, is there anyway to remove it/all dissallowed characters from the filename and still run the above check on the file?

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The name can be parsed with the addslashes() function, if you would like to keep it. Otherwise, I'd strip it using str_replace("'", "", $input);
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    tmp_name shouldn't contain any apostrophes at all, it should just be text. Are you sure the problem is there? Incidentally you can remove all characters that aren't allowed by either hashing the filename or using regular expressions. preg_replace() or ereg_replace().

  • #4
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts
    How would I go about hashing the filename? that would be a pretty good idea.

    I'm pretty sure the problem is with the apostrophes in filename, but along with this check there is a script that makes 2 dynamic thumbnails and another that resizes the uploaded image. Everything works fine except when there is an apostrophe in the filename.

    how would i change

    PHP Code:
    $_FILES['image_file']['tmp_name'
    to show the new hashed file name?


  •  

    Posting Permissions

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