上传文件工作正常,文件大小小于 2.9 MB,但我的 phpinfo (localhost) 显示 upload_max_filesize 64M。
尝试上传较大文件时,表单提交后 $_POST 为空且没有上传文件。
这是我的代码:
<?php
function fileUpload($attachment){
$target_file = UPLOADDIR.basename($attachment["name"]);
if (file_exists($target_file)) {
return "Sorry, file already exists.";
}
if (move_uploaded_file($attachment["tmp_name"], $target_file)) {
return "The file ". basename( $attachment["name"]). " has been uploaded.";
} else {
return $attachment;
return "Sorry, there was an error uploading your file.";
}
}
if(isset($_POST["submit"])) {
fileUpload($_FILES['fileToUpload'])
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
最佳答案
在上传脚本之前,您可以在 php 代码中设置最大上传大小
ini_set('upload_max_filesize', '10M');
或者 您需要在 php.ini 中设置 upload_max_filesize 和 post_max_size 的值:
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
之后运行您的代码
和
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
将其放在 PHP 脚本的顶部,让您的脚本自由发挥!
如果您进行了任何更改或修改 php.ini 文件,您需要重新启动 HTTP 服务器(或本地主机)才能使用新配置。
关于php - 无法使用 max_filesize 发布表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29790347/