脚本无法识别文件扩展名,无法在php中上传pdf

时间:2019-10-28 00:45:36

标签: php

我尝试修改php脚本,以使其上传pdf而不是图像。我已经进行了几次修改,但没有一个工作。如果有人可以向我展示如何修改此脚本以允许上传pdf,我非常感谢。 我是php的新手,需要那里的php专家提供更多指导。 预先感谢您的帮助。

无论如何这是代码:

## Heading ##<html>
<head>
<!-- Bootstrap CSS -->
<link rel="stylesheet" 
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384- 
ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">      

<style>
#uploader {
  width: 300px; 
  height: 200px; 
  background: rgba(0,0,0,.075) url(../../img/pdf_icon.png) repeat fixed left;
  border-radius:8px;
  box-shadow: 5px 5px 10px 0px rgba(0,0,0,0.15);
  display: block;
  padding: 10px;
  }
  #uploader.highlight {
    background:#ff0;
  }
  #uploader.disabled {
    background:#aaa;
  }
  </style>

  <script src="drag-drop-upload-pdf.js"></script>
  </head>
  <body>
  <!-- DROP ZONE -->
  <div id="uploader">
  Muatnaik fail pdf di sini...
  </div>

  <!-- STATUS -->
  <div id="upstat"></div>

  <!-- FALLBACK -->
  <form action="upload-pdf.php" method="post" enctype="multipart/form-data">
    <br />
  <input class="btn btn-primary" type="file" name="file-upload" id="file-upload" accept="pdf/*">
    <br />
    <br />
  <input class="btn btn-primary" type="submit" value="Upload File" name="submit">
  </form>
  </body>
  </html>


## PHP Script ##
<?php
// SOURCE + DESTINATION
$source = $_FILES["file-upload"]["tmp_name"];
$destination = __DIR__.'/../../download/'. $_FILES["file-upload"]["name"];
echo "Uploaded ";
$error = "";

// CHECK IF FILE ALREADY EXIST
if (file_exists($destination)) {
$error = $destination . " already exist.";
}

// ALLOWED FILE EXTENSIONS
if ($error == "") {
$allowed = ["application/pdf", "application/x-pdf", "application/acrobat", "applications/vnd.pdf", 
"text/pdf". "text/x-pdf"];
$ext = strtolower(pathinfo($_FILES["file-upload"]["name"], PATHINFO_EXTENSION));
if (!in_array($ext, $allowed)) {
$error = "$ext file type not allowed. File must be uploaded in PDF format. - " . $_FILES["file- 
upload"]["name"];
  }
}

// LEGIT TEXT FILE CHECK
if ($error == "") {
if (getimagesize($_FILES["file-upload"]["tmp_name"]) == false) {
$error = $_FILES["file-upload"]["name"] . " is not a valid file.";
  }
}

// FILE SIZE CHECK
if ($error == "") {
// 1,000,000 = 1MB
if ($_FILES["file-upload"]["size"] > 50000000) {
$error = $_FILES["file-upload"]["name"] . " - file size too big!";
  }
}

// ALL CHECKS OK - MOVE FILE
if ($error == "") {
if (!move_uploaded_file($source, $destination)) {
$error = "Error moving $source to $destination";
  }
}

// ERROR OCCURED OR OK?
if ($error == "") {
echo $_FILES["file-upload"]["name"] . " Upload DONE.";
} else {
echo $error;
}
?>

1 个答案:

答案 0 :(得分:0)

尝试在HTML中进行更改:

accept =“ application / pdf”