Limit file size and extensions on upload

Discussion in 'PHP' started by invmatt, Mar 10, 2008.

  1. #1
    I've created a simple upload form which renames the file to some random numbers but I keep encountering problems when adding file size/extension limits. This is the current code:

    function findexts ($filename)
    {
    	$filename = strtolower($filename) ;
    	$exts = split("[/\\.]", $filename) ;
    	$n = count($exts)-1;
    	$exts = $exts[$n];
    	return $exts;
    }
    $ext = findexts ($_FILES['uploaded']['name']) ; 
        $ran = rand () ;
        $ran2 = $ran.".";
        $target = "upload/";
        $target = $target . $ran2.$ext; 
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
    echo "http://###.###.com/###/dl.php?file=".$ran2.$ext; //removed URL
    }
    else
    {
    echo "Sorry, there was a problem uploading your file.";
    }
    Code (markup):
    Could anyone possibly help to add the above requirements in?

    Cheers in advance.
     
    invmatt, Mar 10, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    
    $allowed_ext = array('doc','txt','rtf');
    $ext = end(explode('.',$_FILES['uploaded']['name']);
    $ran2 = rand().".";
    $target = "upload/";
    $target = $target . $ran2.$ext; 
    if($_FILES['uploaded']['size'] > 1048576){ $message = 'File over 1048576 bytes';}
    if($message == NULL && !in_array($ext,$allowed_ext)){ $message = 'File extension not allowed';}
    if($message == NULL){
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
    {
    $message =  "http://###.###.com/###/dl.php?file=".$ran2.$ext; //removed URL
    }
    else
    {
    $message = "Sorry, there was a problem uploading your file.";
    }
    }
    
    echo $message;
    
    PHP:
    Peace,
     
    Barti1987, Mar 10, 2008 IP
  3. invmatt

    invmatt Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Parse error: syntax error, unexpected ';' in inc.php on line 24

    Line 24 is

    $ext = end(explode('.',$_FILES['uploaded']['name']);
    PHP:
     
    invmatt, Mar 10, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    There is a missing ")" Change it to:

    
    $ext = end(explode('.',$_FILES['uploaded']['name']));
    
    Code (markup):
    Peace,
     
    Barti1987, Mar 10, 2008 IP