我有一个上传表格,并正在检查文件大小和文件类型,以限制上传的文件为2兆字节和.pdf,.jpg,.gif或.png文件类型。我的目标是如果用户违反了这些规则之一,就会向他们显示一条警告消息。
有四种场景:
不正确的大小/正确的类型(working)
(
使用我当前的代码,当文件大小大于2兆字节(#4)时,即使文件类型是正确的(#3),它也总是显示不正确的“类型”消息。
你知道为什么吗?
if (isset ( $_FILES['uploaded_file'] ) ) {
$file_size = $_FILES['uploaded_file']['size'];
$file_type = $_FILES['uploaded_file']['type'];
if (($file_size > 2097152)){
$message = 'File too large. File must be less than 2 megabytes.';
echo '<script type="text/javascript">alert("'.$message.'");</script>';
}
elseif (
($file_type != "application/pdf") &&
($file_type != "image/jpeg") &&
($file_type != "image/jpg") &&
($file_type != "image/gif") &&
($file_type != "image/png")
){
$message = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
echo '<script type="text/javascript">alert("'.$message.'");</script>';
}
else {
store_uploaded_file($id);
}
}
发布于 2012-02-06 05:56:02
您的代码没有考虑到的是显示多个错误。如上所述,用户可以上传类型错误的文件,但您的代码只能报告其中一个问题。尝试如下所示:
if(isset($_FILES['uploaded_file'])) {
$errors = array();
$maxsize = 2097152;
$acceptable = array(
'application/pdf',
'image/jpeg',
'image/jpg',
'image/gif',
'image/png'
);
if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
}
if((!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"]))) {
$errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
}
if(count($errors) === 0) {
move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');
} else {
foreach($errors as $error) {
echo '<script>alert("'.$error.'");</script>';
}
die(); //Ensure no more processing is done
}
}
查看move_uploaded_file()
的文档(称为move而不是store)以了解更多信息。
发布于 2014-01-02 15:45:32
希望这能有所帮助:-)
if(isset($_POST['submit'])){
ini_set("post_max_size", "30M");
ini_set("upload_max_filesize", "30M");
ini_set("memory_limit", "20000M");
$fileName='product_demo.png';
if($_FILES['imgproduct']['size'] > 0 &&
(($_FILES["imgproduct"]["type"] == "image/gif") ||
($_FILES["imgproduct"]["type"] == "image/jpeg")||
($_FILES["imgproduct"]["type"] == "image/pjpeg") ||
($_FILES["imgproduct"]["type"] == "image/png") &&
($_FILES["imgproduct"]["size"] < 2097152))){
if ($_FILES["imgproduct"]["error"] > 0){
echo "Return Code: " . $_FILES["imgproduct"]["error"] . "<br />";
} else {
$rnd=rand(100,999);
$rnd=$rnd."_";
$fileName = $rnd.trim($_FILES['imgproduct']['name']);
$tmpName = $_FILES['imgproduct']['tmp_name'];
$fileSize = $_FILES['imgproduct']['size'];
$fileType = $_FILES['imgproduct']['type'];
$target = "upload/";
echo $target = $target .$rnd. basename( $_FILES['imgproduct']['name']) ;
move_uploaded_file($_FILES['imgproduct']['tmp_name'], $target);
}
} else {
echo "Sorry, there was a problem uploading your file.";
}
}
发布于 2012-02-06 06:25:27
如果您正在寻找站点上所有上传的硬限制,您可以通过以下设置在php.ini中对其进行限制:
upload\_max\_filesize = 2M
post\_max\_size = 2M
这会将最大上传限制设置为2 MB
https://stackoverflow.com/questions/9153224
复制相似问题