首页 > 解决方案 > “不支持您的文件类型”错误与 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";
  }

}

?>

我的缩进是否可能关闭?

标签: php

解决方案


推荐阅读