php - “不支持您的文件类型”错误与 PHP 代码
问题描述
我正在构建一个网络应用程序,允许您将文件上传到网页,然后将其保存到本地文件夹。
问题是即使上传了可接受的文件类型,第二条if
语句也会呈现错误。Your File Type Not Supported
可以在此处找到接受的文件类型:$allowed = array('jpg', 'jpeg', 'png', 'pdf');
所以我不确定为什么会出现错误。我已经检查并检查了代码中的错误,但似乎找不到它。
这是html代码:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit">Upload File Document</button>
</form>
</body>
</html>
这是PHP代码:
<?php
if (isset($_POST['submit'])) {
$file = $_FILES ['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
#split name and extension of file
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
#check for type of files uploaded
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 9000000) {
#rename uploaded file with unique name
$fileNameNew = uniqid('', true).".".$fileActualExt;
#file destination
$fileDestination = 'uploads/'.$fileNameNew;
#function to change file location
move_uploaded_file($fileTmpName, $fileDestination);
header("Location: index.php?uploadsuccess");
} else {
echo "File is too large";
}
} else {
echo "There was an error uploading your file";
}
} else {
echo "Your File Type Not Supported";
}
}
?>
我的缩进是否可能关闭?
解决方案
推荐阅读
- cocoalumberjack - 将 cocoalumberjack 更新到 3.6.0 后获取“在框架模块中包含非模块化标头”
- google-apps-script - 如何为 onEdit (e) 函数设置更近的范围?
- typescript - js if else loop 怎么能用 ts 缩短技术写
- react-native - 在渲染中使用 Stack navigator 从 app.js 导航
- python - 如何在 Keras 中的预训练 InceptionResNetV2 模型的不同层中找到激活的形状 - Tensorflow 2.0
- html - Safari 从页面加载完整的 html5 视频,并且在加载完成之前不允许使用它
- logging - Kotlin Basics:打印相同的字符串两次以上
- python - 如何使用 Dlib 性别/年龄模型?“.dat 文件”
- android - 测试接口库
- django - int() 参数必须是字符串、类似字节的对象或数字,而不是“ForeignKey”