文件上传条件
File Upload Conditional
我有一个 html 表单,其中包含一些提交到 PHP 页面的文本和文件输入。我想错误检查图像文件输入,但前提是他们选择包含一个。有没有办法检查是否有上传的图像文件?我在下面包含了我的尝试,但它似乎没有用。
if ($_FILES['image'])
{
if ($_FILES['image']['error'] > 0)
{
echo 'Problem: '; }}
switch ($_FILES['image']['error'])
{
case 1: echo 'File exceeded upload_max_filesize.';
break;
case 2: echo 'File exceeded max_file_size.';
break:
case 3: echo 'File only partially uploaded.';
break;
case 4: echo 'No file uploaded.';
break;
default: echo 'cannot upload.';
break;
}
exit;
}
}
If no file is selected for upload in your form, PHP will return $_FILES['userfile']['size'] as 0, and $_FILES['userfile']['tmp_name'] as none.
即:测试 $_FILES 元素将始终 return 为真。您应该改为测试文件大小和临时名称元素。
我有一个 html 表单,其中包含一些提交到 PHP 页面的文本和文件输入。我想错误检查图像文件输入,但前提是他们选择包含一个。有没有办法检查是否有上传的图像文件?我在下面包含了我的尝试,但它似乎没有用。
if ($_FILES['image'])
{
if ($_FILES['image']['error'] > 0)
{
echo 'Problem: '; }}
switch ($_FILES['image']['error'])
{
case 1: echo 'File exceeded upload_max_filesize.';
break;
case 2: echo 'File exceeded max_file_size.';
break:
case 3: echo 'File only partially uploaded.';
break;
case 4: echo 'No file uploaded.';
break;
default: echo 'cannot upload.';
break;
}
exit;
}
}
If no file is selected for upload in your form, PHP will return $_FILES['userfile']['size'] as 0, and $_FILES['userfile']['tmp_name'] as none.
即:测试 $_FILES 元素将始终 return 为真。您应该改为测试文件大小和临时名称元素。