【发布时间】:2016-08-04 16:08:58
【问题描述】:
我试图将大小限制为 50 字节只是为了测试它,但由于某种原因这种方法不起作用。我无权访问 php.ini,如果可能的话,我想用代码来做这件事。
形式:
<form action='uploadFile.php' target='uploadIframe' method='post' enctype='multipart/form-data'>
<div class='fieldRow'>
<label>Select the file: </label>
<input type='file' name='file' id='file' onChange='submitForm1(this)' />
</div>
</form>
<iframe style='border:0;' id='uploadIframe' name='uploadIframe'></iframe>
<div id='successMessage'></div>
上传文件.php
<!doctype html>
<html lang="en">
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<?php
$upload_dir = "../sitedata/auditfiles";
$result["status"] = "200";
$result["message"]= "Error!";
if(isset($_FILES['file'])){
echo "Uploading file... <br />";
if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
$filename = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],
$upload_dir.'/'.$filename);
$result["status"] = "100";
$result["message"]="File was uploaded successfully!";
问题来了:
}elseif ($_FILES["file"]["size"] > 50) {
$result["status"] = "200";
$result["message"]= "Error: Document size exceeds maximum limit of 5 MB. Please reduce the file size and retry upload";
}
//initial code that worked but only does it through php.ini
/* elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE) {
$result["status"] = "200";
$result["message"]= "The file is too big!";/**/
} else {
$result["status"] = "500";
$result["message"]= "Unknown error!";
}
}
?>
</body>
</html>
<script>
$(function () {
$('#successMessage', window.parent.document).html('<?php echo htmlspecialchars($result["message"]); ?>');
});
</script>
function submitForm1(upload_input_field){
//alert("works");
upload_input_field.form.submit();
upload_input_field.disabled = true;
return true;
}
【问题讨论】:
-
快速回答:.htaccess / .user.ini ... 脚本内部不起作用。
-
尝试
if($_FILES["file"]["size"] > 50)然后使用else/elseif,您现在正在做的是首先允许更大的文件,而不是检查它是否首先超过 50。 -
您可以使用 ini_set 而不是实际编辑 php.ini 文件。
-
我以前看过它......所以这不是基于其他帖子的重复。我正在使用 php 5.3。
-
这不是重复的,我找到了答案。这必须在表单内和 之上
标签: php