如何使用php使用在线数据库将PDF文件上传到数据库

时间:2017-08-16 03:05:18

标签: php file-upload

我遵循了教程,下面的代码用于在数据库中插入图像。我想上传PDF文件而不是图片 使用代码我不知道要将PDF文件插入数据库需要更改什么。我找到了另一个有位置的教程。我还想允许用户在任何位置/文件夹中上传PDF。

upload.php

    <!Doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="style.css">
        <title>Success</title>
    </head>
    <body>
    <div id="header">
    <label>Anouncement</label>
    </div>
   <div class="aa">
    <div class="vertical-menu">
    <img src="abc.png" class="img-circle" alt="Ice" width="200" height="200" id="ice">
    <h2 align="center">Milagros Quanico</h2>
    <h3 align="center"><?php
   $servername = "localhost";
    $username = "id834551_teacherss";
    $password = "";
    $dbname = "id834551_teacher";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 


    $conn->close();


    ?></h3>

        <i><a align="center" class="active" href="welcome.php">Home</a></i>
        <i><a align="center" href="Add1.php">Update Topics</a></i>
        <i><a align="center" href="calendar1.php">Calendar</a></i>
        <i><a align="center" href="db.php">#View</a></i>
        <i><a align="center" href="logout.php">Log out</a></i>
        <br><br><br></br></br></br>
    </div>
    <div class="dd"> <!--Content-->
    <br>
    <br>
    <br>
    <h1>Welcome Users </h1>   

<?php
$msg = "";
if (isset($_POST['upload'])) {
   if(getimagesize($_FILES['image']['tmp_name']) == FALSE)
   {
    echo "Please Select an image.";
   }
   else {
    $image = addslashes($_FILES['image']['tmp_name']);
    $name = addslashes($_FILES['image']['name']);
    $image = file_get_contents($image);
    $image = base64_encode($image);
    saveimage($name,$image);
   }
}
displayimage();
function saveimage($name,$image)
{
$servername = "localhost";
    $username = "id834551_teacherss";
    $password = "";
    $dbname = "id834551_teacher";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    $query = "INSERT INTO images (name,images) values ('$name','$image')";
        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $result = mysqli_query($conn,$query);
    if($result) {
        echo "<br/> Image uploaded.";
    }
    else {
        echo "<br/> Image not uploaded.";
    }
}
function displayimage()
{
    $servername = "localhost";
    $username = "id834551_teacherss";
    $password = "jeromelim";
    $dbname = "id834551_teacher";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    $query = "SELECT * FROM images";
       $result = mysqli_query($conn,$query);
    while($row = mysqli_fetch_array($result))
    {
        echo '<img height ="300" width ="300" src="data:image/jpeg;base64,'.$row[2].' "> ';
    }
    mysqli_close($conn);

}
   ?>


    <form method = "post" action = "uploads.php" enctype = "multipart/form-data">
    <input type = "hidden" name ="size" value = "1000000">
    <div>
        <input type = "file" name = "image"/>
    </div>                    
    <div>
        <textarea name = "text" cols="40" rows="4" placeholder="Say Something Here">

        </textarea>
    </div>
    <div>
        <input type = "submit" name="upload" value="Upload File">
    </div>
    </form>
    </div>
    </div>        
    </body>
    </html>

这是将图像插入数据库的实际代码。我也想上传pdf文件

<?php
$msg = "";
if (isset($_POST['upload'])) {
   if(getimagesize($_FILES['image']['tmp_name']) == FALSE)
   {
    echo "Please Select an image.";
   }
   else {
    $image = addslashes($_FILES[''])
    $image = addslashes($_FILES['image']['tmp_name']);
    $name = addslashes($_FILES['image']['name']);
    $image = file_get_contents($image);
    $image = base64_encode($image);
    saveimage($name,$image);
   }
}
displayimage();
function saveimage($name,$image)
{
$servername = "localhost";
    $username = "id834551_teacherss";
    $password = "jeromelim";
    $dbname = "id834551_teacher";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    $query = "INSERT INTO images (name,images) values ('$name','$image')";
        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $result = mysqli_query($conn,$query);
    if($result) {
        echo "<br/> Image uploaded.";
    }
    else {
        echo "<br/> Image not uploaded.";
    }
}
function displayimage()
{
    $servername = "localhost";
    $username = "id834551_teacherss";
    $password = "jeromelim";
    $dbname = "id834551_teacher";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    $query = "SELECT * FROM images";
       $result = mysqli_query($conn,$query);
    while($row = mysqli_fetch_array($result))
    {
        echo '<img height ="300" width ="300" src="data:image/jpeg;base64,'.$row[2].' "> ';
    }
    mysqli_close($conn);

}
   ?>

1 个答案:

答案 0 :(得分:1)

代码已更新...

 <?php
$msg = "";

$tmp_file=$_FILES['image']['tmp_name'];
if($tmp_file){

      $img_name=basename($_FILES['image']['name']);
      $ext = strtolower(substr(strrchr($img_name, "."), 1));
      $ext1=".".$ext;

      $allowed = array('.pdf');

      if(in_array($ext1, $allowed)){

      // Update Path to upload on server

      $img_path="";
      move_uploaded_file($tmp_file,$img_path);
      saveiFILE($img_name,$img_path);


     }else{
         $output_message="Uploaded File seems to be Undefined Type";
     }

}
function saveFILE($name,$image)
{
$servername = "localhost";
    $username = "id834551_teacherss";
    $password = "jeromelim";
    $dbname = "id834551_teacher";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    $query = "INSERT INTO images (name,images) values ('$name','$image')";
        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $result = mysqli_query($conn,$query);
    if($result) {
        echo "<br/> fILE uploaded.";
    }
    else {
        echo "<br/> SOMETHING WENT WRONG";
    }
}
   ?>