Jump to content

Help with adding file size limit to Upload Form


Chrisj

Recommended Posts

I'm trying to add a file size limit to my upload form-code (below)

 

Is this correct?



if ( $_FILES["file"]["size"] < 100000


If that's correct, do I need any additional code to make that line of code functional?

 



$allowedExts = array("gif", "jpeg", "jpg", "pdf", "doc", "docx", "txt", "rtf", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = strtolower( end($temp) );
if (!in_array($extension,$allowedExts))
{
echo ("Error - Invalid File Name");
}
$length = 20;
$randomString = (time());
$thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $thumbnail);
$sql = "INSERT INTO videos ( filename ) VALUES( '$thumbnail' )";
mysql_query($sql);
$file_location = '<a href="http://www.......com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';


Thanks. I look forward to any help.

 

it is and yes you have to make it functional

I would do something like this,

Create an array to hold error messages (Since you could possibly have two - 1 for a wrong extension and 1 for a file that's too large)

 

Check to make sure the file meets the criteria

    $errors     = array();//make an array to hold error messages
    $maxsize    = 100000;//set your max upload size
    
    if($_FILES['file']['size'] > $maxsize) {
        $errors[] = 'Error - File must be less than xx megabytes.';
    }

   if (!in_array($extension,$allowedExts)){
        $errors[] = 'Error - Invalid File Name';       
    }
    if(count($errors) === 0) {
        //process your file upload here
    } else {
        foreach($errors as $error) {
            echo ''.$error.'<br />';
        }
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.