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

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

我试图上传pdf文件到数据库使用mysql和php,但只有像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文件,它不工作。任何人都可以告诉我哪里错了。

yhqotfr8

yhqotfr81#

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

相关问题