如何在php中限制表单提交的文件上传大小

我有一个表格,我希望允许文件等于或小于 50 MB。但是当我上传一个 1.60 MB 的文件时,它说最大文件大小请少上传。任何建议为什么?


文件大小 1.60 MB 如果我打印文件数组它显示大小 = 1686220


// Attempt 1


if (!filesize($file_path) < 50000) {

  // max file size limit reached

}


// Attempt 2


if (!$file['size'] < 50000) {

  // max file size limit reached

}


动漫人物
浏览 102回答 1
1回答

翻阅古今

您也可以jquery在提交之前使用它来做样品:&nbsp; &nbsp; <form action="upload" enctype="multipart/form-data" method="post">&nbsp; &nbsp; &nbsp; &nbsp; Upload image:&nbsp; &nbsp; &nbsp; &nbsp; <input id="image-file" type="file" name="file" />&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Upload" />&nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#image-file').bind('change', function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('This file size is: ' + this.files[0].size/1024/1024 + "MB");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </form>使用 PHP:if (isset ( $_FILES['file'] ) ) {&nbsp; &nbsp; $file_size = $_FILES['file']['size'];&nbsp; &nbsp; $file_type = $_FILES['file']['type'];&nbsp; &nbsp; if (($file_size > 50*1024*1024)){&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $message = 'File too large. File must be less than 50 MB.';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo '<script type="text/javascript">alert("'.$message.'");</script>';&nbsp;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP