PHP PDF文件上传器,IF语句提前删除

时间:2015-02-26 11:09:23

标签: php file pdf if-statement file-upload

我正在尝试创建一个PDF文件上传器,但我的if语句提前删除,否则语句会产生语法错误。有没有办法使这个IF工作?

错误:     

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)

用户进入上传PDF页面的上传页面,然后运行upload_pdf2上传。

Upload.php

<?php
include_once("includes/session.php");
confirm_logged_in();
//include_once("includes/functions_admin.php");
include_once("../includes/masterinclude.php");

$message = "";
$scrolltobottom = "";


$preferences = getPreferences();

//note this will also refresh the page after amending it
$pageTitle = "Site Administration: Image Upload";

include_once("includes/header_admin.php");
?>
<div class="body-indexcontent_admin">
  <div class="admin">
    <br/>
    <h1>Image Upload</h1>
    <br/>


      <div class="login-box">

    <form action="../_cms/upload_pdf2.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input name="image" type="file" id="file" size="50">
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <input type="submit" name="submit" value=" -- Upload PDF -- " class="upload-button">
    </form>

<?php
  include_once("includes/footer_admin.php");
?>

<强> Upload_PDF2.php

<?php
include_once("includes/session.php");
confirm_logged_in();
//include_once("includes/functions_admin.php");
include_once("../includes/masterinclude.php");

$message = "";
$scrolltobottom = "";


$preferences = getPreferences();

//note this will also refresh the page after amending it
$pageTitle = "Site Admin: Image Uploaded";

include_once("includes/header_admin.php");
?>
<div class="body-indexcontent_admin">
  <div class="admin">
    <br/>
    <h1>PDF Uploaded</h1>
    <br/>
    <div class="start-page-content">
        <p>&nbsp;</p>


<?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>";
      }
    } 
?> 

        <p>&nbsp;</p>
    </div>
      <div class="start-page-content">
        <p>Insert the Image URL above in the URL field on the image properties box when using the visual designer.</p>
        <p>&nbsp;</p>
        <h3><a href="javascript: history.go(-1)">Upload another image</a></h3>

    </div>
<?php
  include_once("includes/footer_admin.php");
?>