无法将pdf文件上传到mysql数据库中并无法使用PHP检索它?

时间:2018-11-26 07:27:25

标签: php mysql mysqli phpmyadmin mysql-workbench

我正在尝试使用MySql和PHP将pdf文件上传到数据库,但是只有jpeg / png之类的图像存储在数据库中。我无法将pdf文件上传到数据库并检索它。这是我的HTML代码,带有表单-

<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">
                    <div class="form-row">
                        <div class="name">Name</div>
                        <div class="value">
                            <div class="input-group">
                                <input class="input--style-5" type="text" name="name" required>
                            </div>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="name">Email</div>
                        <div class="value">
                            <div class="input-group">
                                <input class="input--style-5" type="email" name="email" required>
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="name">Phone</div>
                        <div class="value">
                            <div class="input-group">
                                <input class="input--style-5" type="number" name="contact" required>
                            </div>
                        </div>
                    </div>


                    <div class="form-row">
                        <div class="name">Upload Your Resume</div>
                        <div class="value">
                            <div class="input-group">
                                <input class="input--style-5" type="file" name="myfile">
                            </div>
                        </div>
                    </div>
                    <div>
                        <button class="btn" name="btn" type="submit">Submit</button>
                    </div>
                </form>

验证功能低于-

 <script> 
function validate()                                    
{ 
    var name = document.forms["form"]["name"];               
    var email = document.forms["form"]["email"];    
    var phone = document.forms["form"]["contact"];    
    var resume = document.forms["form"]["myfile"];  
   
    if (name.value == "")                                  
    { 
        window.alert("Please enter your name."); 
        name.focus(); 
        return false; 
    } 
   
       
    if (email.value == "")                                   
    { 
        window.alert("Please enter a valid e-mail address."); 
        email.focus(); 
        return false; 
    } 
   
    if (email.value.indexOf("@", 0) < 0)                 
    { 
        window.alert("Please enter a valid e-mail address."); 
        email.focus(); 
        return false; 
    } 
   
    if (email.value.indexOf(".", 0) < 0)                 
    { 
        window.alert("Please enter a valid e-mail address."); 
        email.focus(); 
        return false; 
    } 
   
    if (phone.value == "")                           
    { 
        window.alert("Please enter your telephone number."); 
        phone.focus(); 
        return false; 
    } 
    if(resume.value=="")

    {
    	window.alert("Please upload your resume.");
    	resume.focus();
    	return false;
    }
   
    return true; 
}</script> 

将数据上传到数据库的PHP代码是这个-

<?php
$conn = new PDO('mysql:host=localhost;port=3306;dbname=test',
                  'root',
                  '',
                  array(PDO::ATTR_PERSISTENT => true));
if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
if(isset($_POST['btn']))
{
$name2 = $_POST['name'];
$email2 = $_POST['email'];
$contact2 = $_POST['contact'];
$allowedExts = array("pdf");
$name=$_FILES['myfile']['name'];
$type=$_FILES['myfile']['type'];
$data=file_get_contents($_FILES['myfile']['tmp_name']);
$data = addslashes($data);
$query = "INSERT INTO wp_form (name,email,phone_number,file_name,type,data,description) VALUES (?,?,?,?,?,?,NOW())";
$result = $conn->prepare($query);
$result->execute(array($name2,$email2,$contact2,$name,$type,$data));
header("Location: https://www.example.com/thankyou");
}
?>

从数据库中获取数据的PHP是这个

<!DOCTYPE html>
<html>
<head>
	<title></title>

	<!DOCTYPE html>
<html>
<head>
  <title>Catalog</title>
  <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

</body>
</html>
<?php
$conn = new PDO('mysql:host=localhost;port=3306;dbname=test',
                  'root',
                  '',
                  array(PDO::ATTR_PERSISTENT => true));
if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
}



$query = "SELECT * from wp_form";
$result = $conn->prepare($query);
$result->execute();


echo "<table border='1' cellpadding='0' cellspacing='0'>
    <tr height='50'>
        <td align='center' width='150'><b>S.No</b></td>
        <td align='center' width='150'><b>Name</b></td>
        <td align='center' width='300'><b>Email</b></td>
        <td align='center' width='150'><b>Phone Number</b></td>
        <td align='center' width='150'><b>Resume</b></td>
        <td align='center' width='150'><b>Description</b></td>
    </tr></table>";
while ($row=$result->fetch()) {

    echo "<table border='1' cellpadding='0' cellspacing='0' ><tr height='50'>
        <td align='center' width='150'>".$row['id']."</td>
        <td align='center' width='150'>".$row['name']."</td>
        <td align='center' width='300'>".$row['email']."</td>
        <td align='center' width='150'>".$row['phone_number']."</td>
        <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>
        <td align='center' width='150'>".$row['description']."</td>
      </tr>
</table>";
}
?>

图像正存储在数据库中,但是当我尝试上传pdf文件时,它不起作用。任何人都可以告诉我哪里出问题了。

1 个答案:

答案 0 :(得分:0)

问题

  1. 您要在其中存储文件内容的列的大小和数据类型是什么?您不能使用较小长度的大内容。有时候,这是真正的问题,而不是代码本身。
  2. 为什么不将文件内容转换为十六进制然后保存到数据库?像:$data=file_get_contents($_FILES['myfile']['tmp_name']); $hexa = bin2hex($data); 。然后,您可以使用PHP中的文件流来检索存储的十六进制后,再次创建文件。

希望这会有所帮助。