PHP Upload file code error

Discussion in 'Web Design and Development' started by tony811, Feb 8, 2012.

  1. tony811 macrumors newbie

    Joined:
    Feb 8, 2012
    #1
    Hi i have just finished a tutroial on how to upload a file using PHP however i am getting a very simple error i understand and just cant resolve the error it is

    Notice: Use of undefined constant extension - assumed 'extension' in C:\wamp\www\File Upload\upload.php on line 11 (Highlighted in Red)

    I know its to do with variable extension not being declared however my PHP coding skills are limited. And help would be great.



    upload.php code is
    <?php

    $uploaddir = "uploads/images";
    $allowed_ext = "jpg, JPG, png, gif";
    $max_size = "500000000";
    $max_width = "4000";
    $max_height = "4000";


    $extension = pathinfo($_FILES['file']['name']);
    $extension = $extension[extension];
    $allowed_paths = explode(", ",$allowed_ext);

    for($i = 0; $i < count($allowed_paths); $i++){
    if ($allowed_paths[$i] == "$extension"){
    $ok = "1";
    }
    }

    if ($ok == "1"){
    if($_FILES['file']['size'] > $max_size)
    {
    print "File size is to big !";
    exit;
    }

    if($max_width && $max_height){
    list($width, $height, $type, $w)=
    getimagesize($_FILES['file']['tmp_name']);
    if($width > $max_width || $height > $max_height)
    print "File height and or width are too big ! ";
    exit;
    }
    }

    if(is_uploaded_file($_FILES['files']['tmp_name']))
    {
    move_uploaded_file($_FILES['files']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    print "Your file has been uploaded successfully !";
    }

    else
    {
    print "Incorrect file extension !";
    }
    ?>


    index.php

    <table width="500" height ="500" border="5" align = "center" cellpadding="10">
    <tr>
    <tr>
    <td valign="top">
    <form action ="upload.php" method = "post" enctype="multipart/form-data">
    File:<input type="file" name ="file" size "30"><input type="submit" value="upload">
    </form>
    </td>
    </tr>
    </table>


    Any suggestions would be much appricated

    Thanks in Advance
     
  2. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
  3. tony811 thread starter macrumors newbie

    Joined:
    Feb 8, 2012
    #3
    Thanks Darth , i just get a blank page and no messages the error has gone however i dont believe it is doing its function.
     
  4. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #4
    • You didn't enclose your if clause in curly braces on the extension check, so your script was exiting every time. Hence, no errors.
    • You mistyped the $_FILES['file']['tmp_name'] a couple of times.
    Try this:
    PHP:
    <?php
    $uploaddir 
    "uploads/images";
    $allowed_ext = array(
        
    'jpg',
        
    'jpeg',    // It's a good idea to include this version of the jpeg extension as well.
        
    'JPG',
        
    'png',
        
    'gif'
    );
    $max_size "500000000";
    $max_width "4000";
    $max_height "4000";


    $info pathinfo($_FILES['file']['name']);
    $extension $info['extension'];

    if (!
    in_array($extension,$allowed_ext))
    {
        print 
    "Incorrect file extension !";
        exit;
    }

    if(
    $_FILES['file']['size'] > $max_size)
    {
        print 
    "File size is too big !";
        exit;
    }

    if(
    $max_width && $max_height)
    {
        list(
    $width$height$type$w)=
            
    getimagesize($_FILES['file']['tmp_name']);

        if(
    $width $max_width || $height $max_height)
        {
            print 
    "File height and or width are too big ! ";
            exit;
        }
    }

    if (
    move_uploaded_file($_FILES['file']['tmp_name'],
        
    $uploaddir.'/'.$_FILES['file']['name']))
    {
        print 
    $_FILES['file']['name']." has been uploaded successfully !";
    }

    else
    {
        print 
    "There was a problem uploading ".$_FILES['file']['name'];
    }
    Hint: make sure your upload directory has the correct permissions set so the web server can write to it.
     

Share This Page