Adding file type validation to php upload script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • underground
    New Member
    • Sep 2006
    • 41

    Adding file type validation to php upload script

    I have a script that I have modified to upload image to mysql but I am concerned about the security risk envovled with allowing user to upload files to a mysql..

    I've been trying to modify the php to include validation for a jpg and gif but to not avail could someone point me in the direction I need to move with a visual sample

    Thank's as all way!
    [PHP]
    if(isset($_POST['upload']) && $_FILES['userfile']['size']> 0)
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $fp = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpNa me));
    $content = addslashes($con tent);
    fclose($fp);
    if(!get_magic_q uotes_gpc())
    {
    $fileName = addslashes($fil eName);
    }
    include '../../config.php';
    include '../../opendb.php';

    $query = "INSERT audio SET name='$fileName ', size='$fileSize ', type='$fileType ', content='$conte nt' WHERE username='$user name'";

    mysql_query($qu ery) or die(your upload could be done file already exist');
    include '../../closedb.php';

    echo "<br>File $fileName uploaded";
    }
    [/PHP]
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Check out w3schools upload script (with validation)

    Regards

    Comment

    • underground
      New Member
      • Sep 2006
      • 41

      #3
      Originally posted by markusn00b
      This is some really good info I'll will check later and post back tonight!

      Comment

      • underground
        New Member
        • Sep 2006
        • 41

        #4
        Originally posted by underground
        This is some really good info I'll will check later and post back tonight!
        Sorry it took so long but the the advice worked perfectly and I now know how to validate file upload

        Thanks You So Much.

        I will post another validation for different query later today though.

        Comment

        Working...