无法将pdf文件上载到mysql数据库并使用php检索它?

aiazj4mn  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(411)

我试图上传pdf文件到数据库使用mysql和php,但只有像jpeg/png的图像存储在数据库中。我未能将pdf文件上载到数据库并检索它。这是我的html代码和表单-

  1. <form id="form" name="form" onsubmit="return validate()" action="http://localhost:81/wordpress/wp-content/themes/themify-ultra/submit.php" enctype="multipart/form-data" method="POST">
  2. <div class="form-row">
  3. <div class="name">Name</div>
  4. <div class="value">
  5. <div class="input-group">
  6. <input class="input--style-5" type="text" name="name" required>
  7. </div>
  8. </div>
  9. </div>
  10. <div class="form-row">
  11. <div class="name">Email</div>
  12. <div class="value">
  13. <div class="input-group">
  14. <input class="input--style-5" type="email" name="email" required>
  15. </div>
  16. </div>
  17. </div>
  18. <div class="form-row">
  19. <div class="name">Phone</div>
  20. <div class="value">
  21. <div class="input-group">
  22. <input class="input--style-5" type="number" name="contact" required>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="form-row">
  27. <div class="name">Upload Your Resume</div>
  28. <div class="value">
  29. <div class="input-group">
  30. <input class="input--style-5" type="file" name="myfile">
  31. </div>
  32. </div>
  33. </div>
  34. <div>
  35. <button class="btn" name="btn" type="submit">Submit</button>
  36. </div>
  37. </form>

验证函数如下-

  1. <script>
  2. function validate()
  3. {
  4. var name = document.forms["form"]["name"];
  5. var email = document.forms["form"]["email"];
  6. var phone = document.forms["form"]["contact"];
  7. var resume = document.forms["form"]["myfile"];
  8. if (name.value == "")
  9. {
  10. window.alert("Please enter your name.");
  11. name.focus();
  12. return false;
  13. }
  14. if (email.value == "")
  15. {
  16. window.alert("Please enter a valid e-mail address.");
  17. email.focus();
  18. return false;
  19. }
  20. if (email.value.indexOf("@", 0) < 0)
  21. {
  22. window.alert("Please enter a valid e-mail address.");
  23. email.focus();
  24. return false;
  25. }
  26. if (email.value.indexOf(".", 0) < 0)
  27. {
  28. window.alert("Please enter a valid e-mail address.");
  29. email.focus();
  30. return false;
  31. }
  32. if (phone.value == "")
  33. {
  34. window.alert("Please enter your telephone number.");
  35. phone.focus();
  36. return false;
  37. }
  38. if(resume.value=="")
  39. {
  40. window.alert("Please upload your resume.");
  41. resume.focus();
  42. return false;
  43. }
  44. return true;
  45. }</script>

将数据上传到数据库的php代码是这样的-

  1. <?php
  2. $conn = new PDO('mysql:host=localhost;port=3306;dbname=test',
  3. 'root',
  4. '',
  5. array(PDO::ATTR_PERSISTENT => true));
  6. if (!$conn) {
  7. die("Connection failed: " . mysqli_connect_error());
  8. }
  9. echo "Connected successfully";
  10. if(isset($_POST['btn']))
  11. {
  12. $name2 = $_POST['name'];
  13. $email2 = $_POST['email'];
  14. $contact2 = $_POST['contact'];
  15. $allowedExts = array("pdf");
  16. $name=$_FILES['myfile']['name'];
  17. $type=$_FILES['myfile']['type'];
  18. $data=file_get_contents($_FILES['myfile']['tmp_name']);
  19. $data = addslashes($data);
  20. $query = "INSERT INTO wp_form (name,email,phone_number,file_name,type,data,description) VALUES (?,?,?,?,?,?,NOW())";
  21. $result = $conn->prepare($query);
  22. $result->execute(array($name2,$email2,$contact2,$name,$type,$data));
  23. header("Location: https://www.example.com/thankyou");
  24. }
  25. ?>

从数据库获取数据的php是这样的-

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <title>Catalog</title>
  9. <!-- Latest compiled and minified CSS -->
  10. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  11. <!-- jQuery library -->
  12. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  13. <!-- Latest compiled JavaScript -->
  14. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  15. </head>
  16. <body>
  17. </body>
  18. </html>
  19. <?php
  20. $conn = new PDO('mysql:host=localhost;port=3306;dbname=test',
  21. 'root',
  22. '',
  23. array(PDO::ATTR_PERSISTENT => true));
  24. if (!$conn) {
  25. die("Connection failed: " . mysqli_connect_error());
  26. }
  27. $query = "SELECT * from wp_form";
  28. $result = $conn->prepare($query);
  29. $result->execute();
  30. echo "<table border='1' cellpadding='0' cellspacing='0'>
  31. <tr height='50'>
  32. <td align='center' width='150'><b>S.No</b></td>
  33. <td align='center' width='150'><b>Name</b></td>
  34. <td align='center' width='300'><b>Email</b></td>
  35. <td align='center' width='150'><b>Phone Number</b></td>
  36. <td align='center' width='150'><b>Resume</b></td>
  37. <td align='center' width='150'><b>Description</b></td>
  38. </tr></table>";
  39. while ($row=$result->fetch()) {
  40. echo "<table border='1' cellpadding='0' cellspacing='0' ><tr height='50'>
  41. <td align='center' width='150'>".$row['id']."</td>
  42. <td align='center' width='150'>".$row['name']."</td>
  43. <td align='center' width='300'>".$row['email']."</td>
  44. <td align='center' width='150'>".$row['phone_number']."</td>
  45. <td align='center' width='150'><a target='_blank' href='http://localhost:81/wordpress/wp-content/themes/themify-ultra/view.php?id=".$row['id']."'>".$row['file_name']." </a></td>
  46. <td align='center' width='150'>".$row['description']."</td>
  47. </tr>
  48. </table>";
  49. }
  50. ?>

图像存储在数据库中,但当我试图上传pdf文件,它不工作。任何人都可以告诉我哪里错了。

yhqotfr8

yhqotfr81#

问题是,
要存储文件内容的列的大小和数据类型是什么?您不能使用较小长度的大内容。有时,这才是真正的问题,而不是代码本身。
为什么不把文件的内容转换成十六进制,然后保存到数据库中呢?比如: $data=file_get_contents($_FILES['myfile']['tmp_name']); $hexa = bin2hex($data); . 然后,在php中使用文件流检索存储的十六进制文件之后,您可以再次创建该文件。
希望能有所帮助。

相关问题