我试图将大小限制为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>
uploadFile.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;
}
答案 0 :(得分:3)
upload_max_filesize
处于PHP_INI_PERDIR
模式,并且来自PHP文档:
PHP_INI_PERDIR 可以在php.ini,.htaccess,httpd.conf或.user.ini中设置条目(自PHP 5.3开始)
因此,您的选择是php.ini,.htaccess,httpd.conf或.user.ini,您无法使用ini_set
在脚本中设置它。