Why wont my image file upload script work

Discussion in 'PHP' started by Imozeb, Jun 29, 2010.

  1. #1
    This is my code.

    Javascript is to show the fileupload box.

    PHP is to check if the file type is .gif

    It always gives an error.

    Any idea why this would happen. I checked the file name array and it seems to not exist.


    Javascript Code:
    
    if(document.getElementById('img_file').innerHTML == '')
    		{
    			document.getElementById('img_file').innerHTML = 'Browse To The Image File (.gif).<br/><input name="imgfilename" type="file" id="imgfilename" class="textbox" />';
    		}
    
    
    Code (markup):
    PHP code
    $fileext = pathinfo($_FILES['imgfilename']['name'], PATHINFO_EXTENSION);
    				$fileext = strtolower($fileext);
    				$allowedexts = array("gif");
    				if(in_array($fileext, $allowedexts)) 
    				{
    				}
    				else
    				{
    					$imgerror = 'Please choose a valid image file to upload! File type must be .gif';
    					$showfail = 1;
    				}
    				if($_FILES['imgfilename']['size'] > 20000)
    				{
    					$imgerror = 'Please choose a valid file to upload! Max size is 20KB';
    					$showfail = 1;
    				}
    Code (markup):
     
    Imozeb, Jun 29, 2010 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    Check that you have this in your form tag:

    enctype="multipart/form-data"

    If yes then what content do you get when you print_r($_FILES); within php on submit.
     
    mfscripts, Jun 29, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ah! Thanks. I can't believe I forgot that simple thing. :)
     
    Imozeb, Jun 29, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Also, change:

    			if(in_array($fileext, $allowedexts)) 
    				{
    				}
    				else
    				{
    					$imgerror = 'Please choose a valid image file to upload! File type must be .gif';
    					$showfail = 1;
    				}
    
    PHP:
    Too:

    			if(!in_array($fileext, $allowedexts)) {
    					$imgerror = 'Please choose a valid image file to upload! File type must be .gif';
    					$showfail = 1;
    				}
    
    PHP:
    its cleaner.
     
    danx10, Jun 29, 2010 IP