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.

 

Link to comment
Share on other sites

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 />';
        }
    }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.