-1

Hi Can anyone tell me how to restict user from uploading the video files of large size and they should upload only specific type. They should be able to upload only 8mb files and not more than that. Below is my code.

        <?php
    if(isset($_POST['submit']))
    {

        $filename=$_FILES['file']['name'];
        $filesize=$_FILES['file']['size'];
        $filetype=$_FILES['file']['type'];
        $tmpfile=$_FILES['file']['tmp_name'];
        $unique=str_shuffle("abcde").$filename;

        if ((($filetype == "video/avi")
    || ($filetype == "video/mpeg")
    || ($filetype == "video/mpg")
    || ($filetype == "video/mov")|| ($filetype == "video/wmv") || ($filetype == "video/rm") || ($filetype == "image/mp4"))
    && ($filesize < 8388608 and $filesize > 20))

{

    move_uploaded_file($tmpfile,"uploads/$unique");

        echo'file is uploaded';
}
        else
    {
        echo'failure in uploading';
    }





}


?>

Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
Ujjwal Chowdary
  • 125
  • 2
  • 5
  • 18

1 Answers1

0

You can also limit the file size in the HTML Form: <input type="hidden" name="MAX_FILE_SIZE" value="8388608" /> although it will process the entire upload and then result in a failure.

You're best bet is to use jQuery to evaluate the selected file on Submit, with PHP server-side validation like you have as well.

EDIT1a: jQuery example: Using jQuery, Restricting File Size Before Uploading

EDIT1b: Example PHP Upload Code from working site:

protected function handle_image_upload($frmFilesID = false, $thisFile = false) {

$fileName = $_FILES["$frmFilesID"]['name'][$thisFile];
$tmpName = $_FILES["$frmFilesID"]['tmp_name'][$thisFile];
if (!is_uploaded_file($tmpName)) { trigger_error("Security block.", E_USER_ERROR); return false; }
$fileSize = $_FILES["$frmFilesID"]['size'][$thisFile];
$fileType = $_FILES["$frmFilesID"]['type'][$thisFile];

// Evaluate File Size
if ($fileSize > '8388608') { trigger_error('File exceeds maximum size.', E_USER_ERROR); return false; }

// Make folder path if needed
$destPath = 'imgdir';
if (!is_dir($destPath) && !@mkdir($destPath, 0777, true)) { trigger_error("Could not create folder.", E_USER_ERROR); return false; }

// Test if file already exists (auto-rename?)
if (file_exists($destPath . '/' . $fileName)) {
    trigger_error("File already exists, rename the file and try again.", E_USER_ERROR);
    return false;
}

// Move uploaded file from tmp
if (move_uploaded_file($tmpName, $destPath . '/' . $fileName)) {
    return true;
} else {
    trigger_error("Invalid results.", E_USER_NOTICE);
    return false;
}

}

Call the function for each file being uploaded:

for ($i=0; $i<$count; $i++) {
    $frmFilesID = "ImagesFormIdGoesHere";
    if (!handle_image_upload($frmFilesID, $i)) { echo "<br /><strong>NOTE:</strong> Failed to upload file $i..."; }
}
Community
  • 1
  • 1
Brock Hensley
  • 3,617
  • 2
  • 29
  • 47