您好任何人都可以告诉我如何停止用户上传大尺寸的视频文件,他们应该只上传特定类型。他们应该只能上传8mb文件而不能更新。以下是我的代码。
<?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';
}
}
?>
答案 0 :(得分:0)
您还可以在HTML表单中限制文件大小:<input type="hidden" name="MAX_FILE_SIZE" value="8388608" />
,但它会处理整个上传,然后导致失败。
您最好的选择是使用jQuery来评估Submit上的选定文件,并使用PHP服务器端验证。
EDIT1a: jQuery 示例:Using jQuery, Restricting File Size Before Uploading
EDIT1b:来自工作现场的PHP上传代码示例:
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;
}
}
为每个上传的文件调用该函数:
for ($i=0; $i<$count; $i++) {
$frmFilesID = "ImagesFormIdGoesHere";
if (!handle_image_upload($frmFilesID, $i)) { echo "<br /><strong>NOTE:</strong> Failed to upload file $i..."; }
}