我在 PHP 中有一个文件(图像)上传脚本,用于上传和调整图像大小......它使用简单的 MIME 类型和大小验证,因此只允许 jpg 图像和 1MB 最大文件大小。
我最近发现了一个问题。当我尝试使用脚本上传 .avi 文件时,脚本会像处理正确的 MIME 类型和大小一样处理文件,然后什么也不做,只是将我带回上传表单而没有任何错误消息。(而不是显示“文件太大”消息)。
我的意思是,如果我尝试上传 .gif 或 .txt 或其他内容,我会得到一个错误,正如预期的那样。如果我尝试上传任何大于 1MB 的文件,我会得到一个错误,正如预期的那样。只有当我尝试上传超过 1MB 的 .avi 文件时,我才没有收到任何错误......好吧,这里是代码的第一部分:
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 1024000);
if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/Wamp/www/Version-1.4/posters_uploaded/');
// replace any spaces in original filename with underscores. At the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'kb';
// create an array of permitted MIME types
$permitted = array('image/jpeg','image/pjpeg');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0: // ...................
我只是在修改构建 PHP 代码,所以我不是专家......有什么建议吗?谢谢。