如何在php mysql中上传pdf和图像

时间:2018-03-09 10:00:44

标签: php mysql upload

我正在尝试在php mysql中上传pdf文件和图像,但似乎move_uploaded_file函数只能用于一个文件。我试图让它适用于这两个文件,但它似乎对我来说不起作用。它只将图像移动到目标文件夹,并将图像名称和pdf名称添加到数据库,但它不会将pdf移动到目标文件夹。这是代码。请帮忙

    <?php
    session_start();
    require_once("includedfunctions.php");
    include 'dbh.php';

    if (isset($_POST['submit'])){
    $title=$_POST['title'];
    $author=$_POST['author'];
    $target = "img/pic_book/";
    $target2 = "img/pdf/";
    $imgname = basename($_FILES["cphoto"]["name"]);
    $bookname = basename($_FILES["book"]["name"]);
    $newname = $target.basename($_FILES["cphoto"]["name"]);
    $newname2 = $target2.basename($_FILES["book"]["name"]);
    $img_type = pathinfo($newname,PATHINFO_EXTENSION);
    $book_type = pathinfo($newname2,PATHINFO_EXTENSION);
    if($img_type!='jpg' && $img_type!='JPG' && $img_type!='png' && $img_type!='PNG'){
        $message="Please ensure you are entering an image";
    }else{
        if($book_type != 'pdf'){
            $message="books must be uploaded in pdf format";    
        }else{
            if(!preg_match("/^[a-zA-Z'-]+$/",$author)){
                $message = "<p style='color:red'>Please enter the real name of the author; not a nickname.</p>";
            }else{
            if (move_uploaded_file($_FILES["cphoto"]["tmp_name"], $newname)) {
                    if(move_uploaded_file($_FILES["book"]["tmp_name"], $newname2));{
                    $sql = "INSERT INTO books (Title, Author, pathtopdf, pathtoimage) VALUES ('$title', '$author', '$bookname', '$imgname')";
                    $result = mysqli_query($conn, $sql);
                    if($result){
                        $message = "upload successful";
                    }else{
                        $message = "upload failed1";
                    }
                    }       
            }else{
                $message = "upload failed";
            }
            }
        }
    }
}
else{
  $message="";
  $title="";
}
?>


<html>
<head>
<title>Libraria</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/contactcss.css" rel="stylesheet">
    <script src="js/respond.js"></script>
</head>

<body>
    <br><br><br><br><br>
<!-- content -->
<div class="container">
    <?php 
        echo '<p>Welcome ' . $_SESSION['name']. '</p><br>';
        echo '<p>' . $message. ' </p>';
    ?>  
    <br><br>
    <!--form-->
    <div class="row"> 
        <form action="admin2.php" method = "post" enctype="multipart/form-data">
          <label for="Title">Title</label><br>
          <input type="text" id="fname" value ="<?php echo $title; ?>" name="title" placeholder="Title of the book" required><br>
          <label for="author">Author</label><br>
          <input type="text" id="lname" name="author" placeholder="Author of the book" required><br>
          <label for="Cover photo">Cover photo</label><br>
          <input type="file" id="cphoto" name="cphoto"  required><br>
          <label for="book">Book</label>
          <input type="file" id="book" name="book" required><br>
          <button class="submit" type="submit" name="submit"><b>Upload</b></button>
        </form>
    </div>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

代码完全正确。只需查看pdf大小。如果大小超过4MB,则不允许上传。你需要在php.ini文件或apache配置设置文件中增加上传文件大小。

度过美好的一天:)

答案 1 :(得分:0)

application / pdf是pdf的mime类型,而不仅仅是pdf。

相关问题