SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image upload script -- check extension?

    Hello,

    Does anyone know if its possible to find the filename or extension of the file after using:

    HTML Code:
    <input type="file" name="thefile">
    I'm trying to write an image upload script but I need it to check the extension to make sure its a supported image file (i.e. png, jpg, gif etc.)

    Basically it will work like this:
    Code:
    Form submitted-> Check extension & size -> other processing... ->MySQL DB
    Also, is it possible for PHP to check and show the size of the file being uploaded?

    Thanks in advance

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Check file extension: substr
    Check if file is an image: getimagesize
    Check for specific image type from file headers: exif_imagetype
    Check size of a file: filesize

  3. #3
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at the superglobal $_FILES

    or check the manual
    http://bg2.php.net/manual/en/features.file-upload.php

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a basic image upload script: http://dev.paulb.ca/upload.phps

    This is the part that checks the file extension:

    PHP Code:
    $explodedfile explode('.'$fileName);
    $extension $explodedfile[count($explodedfile)-1];
    if(!(
    $extension == 'jpeg' || $extension == 'jpg' || $extension == 'gif' || $extension == 'png')){
      echo 
    "invalid file";
      
    // might want to log the IP here, along with $extension could be a hacker attack
      
    exit();



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
  •