PDF文件上传器与PHP

时间:2015-02-26 09:37:57

标签: php pdf file-upload

我正在尝试使用php MIME类型上传PDF文件。单击上载时,代码产生以下错误:

  

解析错误:语法错误,第44行/home/stylecraft/domains/stylecraft-yarns.co.uk/public_html/dev/_cms/upload_pdf2.php中的意外T_ELSE

代码:

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png", "pdf");
$extension = end(explode(".", $_FILES["file"]["name"]));

if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    die("Upload failed with error " . $_FILES['file']['error']);
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['file']['tmp_name']);
$ok = false;
switch ($mime) {
   case 'image/jpeg':
   case 'application/pdf':
        $ok = true;
   default:
       die("Unknown/not permitted file type");
}

  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("../pdf/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists: ";
      echo "<p>Image URL: <strong>" . $preferences->PREF_SHOPURL . "/pdf/" . $_FILES["file"]["name"] . "</strong></p>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../pdf/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "pdf/" . $_FILES["file"]["name"];
      echo "<p>&nbsp;</p>";
      echo "<p>PDF URL: <strong>" . $preferences->PREF_SHOPURL . "/pdf/" . $_FILES["file"]["name"] . "</strong></p>";
      }
    }

else
  {
  echo "Invalid file";
  }
?> 

1 个答案:

答案 0 :(得分:0)

那是因为你的代码中没有相应的 if 语句:
else 语句应该有一个相应的“伙伴”,即if。

你有:

if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    ...
}
else {
    ...
}

你还有另外一个:

else {
}

在您的代码中,上一个else之前没有if,这就是您收到错误的原因