作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想弄清楚为什么这个上传脚本失败了。
从 HTML 表单开始:
<form role="form" action="api/upload.php" method="post" enctype="multipart/form-data">
<input id="file" name="file" type="file" />
<input class="btn btn-primary" type="submit" value="Upload" />
</form>
这是 PHP 脚本:
<?php
if(isset($_FILES['file'])){
$file = $_FILES['file'];
$target_file = basename($_FILES["file"]["name"]);
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode('.',$file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('txt', 'jpg', 'xlsx', 'pptx', 'docx', 'doc', 'xls', 'pdf');
if(in_array($file_ext, $allowed)){
if($file_error === 0){
if($file_size <= 99600000){ // this was set to 600000
$file_name_new = uniqid('', true) . '.' . $file_ext;
$file_destination = '../files/' . $file_name;
if(move_uploaded_file($file_tmp, $file_destination)){
header("Location: ../index.php");
die();
}
else{
echo "There was a problem uploading the file";
}
}
else{
echo "The file is too large";
}
}
else{
echo "There was an error uploading the file";
}
}
else{
echo "The file type is not allowed";
}
}
?>
请原谅嵌套的 IF 语句。我在 youtube 上关闭了这个视频:https://www.youtube.com/watch?v=PRCobMXhnyw
上面的代码有效。我可以上传文件,当发生错误时,我会收到相应的错误消息。
但是,我遇到了无法上传的文件。这是一个允许的文件,一个 word 文档,恰好是 14MB。不确定这是否太大。但即便如此,我尝试上传的太大的文件仍无法通过 file_size 检查,并且我会收到相应的错误消息。
在这种情况下,我得到的只是一个空白屏幕。我可以在初始 IF 语句之前和之后回显“你好”,但它在第一个 IF 之后立即失败。
最佳答案
您应该简单地增加 upload_max_filesize
的值(默认情况下它是 2M
)在你的 php.ini
文件中(它的位置取决于你的操作系统),然后重新启动你的网络服务器。
关于PHP 上传 14MB 文件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39167937/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!