我试图弄清楚为什么这个上传脚本失败了。
从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>
Run Code Online (Sandbox Code Playgroud)
这是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";
}
}
?>
Run Code Online (Sandbox Code Playgroud)
请原谅嵌套的IF语句。我正在youtube上观看此视频:https : //www.youtube.com/watch?v=PRCobMXhnyw
上面的代码有效。我可以上载文件,并且在发生错误时,我会得到适当的错误消息。
但是,我遇到了无法上传的文件。它是一个允许的文件,一个Word文档,恰巧是14MB。不知道这是否太大。但即使如此,我尝试上传的文件过大也不会通过file_size检查,并且会得到相应的错误消息。
在这种情况下,我得到的只是一个空白屏幕。我可以在初始IF语句之前和之后回显“ hello”,但在第一个IF之后立即失败。
| 归档时间: |
|
| 查看次数: |
120 次 |
| 最近记录: |