PHP文件上传适用于某些但不是所有具有相同文件类型的文件


php file upload working for some but not all files with same file type

我正在使用以下php文件上传脚本上传文件,创建一个文件夹目录并将文件存储在一个文件夹中,然后将该文件的详细信息记录在我的数据库中。

我的

问题是这适用于我的一个 PDF 文档 aroun 54kb,但是当我尝试上传大约 5mb 的较大 pdf 时,它显示无效文件类型。 此外,我在上传文档文件类型时遇到问题。我不知道这是否是因为文档可以是docx或doc,但如果可能的话,我的脚本应该支持这两种类型。

有人可以告诉我我哪里出错了吗?谢谢

<?php
session_start();
include("config.php");

$supplier_name = $_POST['supplier_name'];
$supplier_number = $_POST['supplier_number'];
$start_date = date('Y-m-d', strtotime($_POST['start_date']));
$end_date = date('Y-m-d', strtotime($_POST['end_date']));
$notice = $_POST['notice'];
$steak = $_POST['steak'];
$supplier_name = stripslashes($supplier_name);
$supplier_name = mysql_real_escape_string($supplier_name);
$supplier_number = stripslashes($supplier_number);
$supplier_number = mysql_real_escape_string($supplier_number);
$start_date = stripslashes($start_date);
$start_date = mysql_real_escape_string($start_date);
$end_date = stripslashes($end_date);
$end_date = mysql_real_escape_string($end_date);
$notice = stripslashes($notice);
$notice = mysql_real_escape_string($notice);
$steak = stripslashes($steak);
$steak = mysql_real_escape_string($steak);
$rand = "HC" . substr(md5(microtime()),rand(0,26),5);
mkdir("../data/contracts/$rand");   
$allowedExts = array("pdf", "doc");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/doc")
&& ($_FILES["file"]["size"] < 10000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 10000) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("../data/contracts/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {

      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../data/contracts/$rand/" . $_FILES["file"]["name"]);
     $sql = "INSERT INTO supplier_contracts (id, supplier_name, supplier_number, contract_number, contract_start, contract_end, contract_termination_period, date, owner, steak_holder) VALUES ('', '$supplier_name', '$supplier_number','$rand', '$start_date', '$end_date', '$notice', now(), '{$_SESSION['user']}', '$steak')";
    $result2 = mysql_query($sql);
    if($result2) {
        $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">&#10006;</div><h23>Thank You!</h23><p>Your Contract was successfully created. Your Contract number is: '.$rand.'.</p></div>';
        header('Location: ' . $_SERVER['HTTP_REFERER']);
    }else{
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">&#10006;</div><h23>Oooops!</h23><p>There was an error trying to create this Contract. Please try again later.</p></div>';
    header('Location: ' . $_SERVER['HTTP_REFERER']);
    }
  } } }else{
      $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">&#10006;</div><h23>Oooops!</h23><p>There was an error trying to create this Contract. Please try again later.</p></div>';
    header('Location: ' . $_SERVER['HTTP_REFERER']);

    } 
?>

如果您创建了一个自定义php.ini(如下面的代码),请将其直接放入 php 文件所在的文件夹中。

file_uploads = On
upload_max_filesize = 10M
post_max_size = 10M

要接受其他类型,您必须将它们添加到 allowedExts 数组中,例如:

$allowedExts = array("pdf", "doc", "docx", "xlsx");