我想弄清楚为什么这个上传脚本失败了。
从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文档,恰好是14 to。不知道是不是太大了。但是,即使我试图上传的文件太大,也无法通过file_size检查,而且我也会得到相应的错误消息。
在这种情况下,我得到的只是一个空白的屏幕。我可以在最初的IF语句之前和之后回显'hello‘,但是它在第一个IF之后就失败了。
发布于 2016-08-26 05:54:27
您只需在您的upload_max_filesize
文件(其位置取决于您的操作系统)中增加php.ini
(默认为2M
)的值,并重新启动您的web服务器。
https://stackoverflow.com/questions/39167937
复制