【发布时间】:2017-01-03 05:21:27
【问题描述】:
我正在尝试找出此上传脚本失败的原因。
从 HTML FORM 开始:
<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 语句之前和之后回显“hello”,但在第一个 IF 语句之后它就失败了。
【问题讨论】:
-
您是否尝试查看
$_FILES['file']['error']中的代码,如果它不为零,那么它将指向您一条错误消息 -
我无法在第一个 IF 语句之后回显任何内容。还有其他方法可以检查 $file['error'] 吗?
-
为什么什么都不能回显...
-
检查你的 PHP upload_max_filesize secure.php.net/manual/en/ini.core.php#ini.upload-max-filesize
-
任何文件上传的第一个测试应该是看
$_FILES['file']['error']看first user contribution in the Manual Page for an example