-1

I am trying to upload pdf file into database using MySql and PHP but only images like jpeg/png are getting stored in the database. I failed to upload pdf file into the database and retrieve it. Here is my HTML code with form-

<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>

Validate function is below-

 <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 code to upload data into the database is this-

<?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 to fetch data from the database is this-

<!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>";
}
?>

Images are getting stored in the database but when I am trying to upload pdf file it doesn't work. Anyone can please tell me where I am getting wrong.

Sahil Verma
  • 53
  • 1
  • 1
  • 6

1 Answers1

0

Question,

  1. What is the size and datatype of the column where you want to store the content of the file? you can't use large content in a smaller length. Some times, this is the real problem and not the code itself.
  2. Why not convert the content of the file to hexa decimal then save it to the database? like: $data=file_get_contents($_FILES['myfile']['tmp_name']); $hexa = bin2hex($data); . Then you can just create the file again after you retrieve the stored hexadecimal using file streaming in PHP.

Hope that it will help.

Itami
  • 78
  • 1
  • 8
  • Does your file exceed 5mb? Perhaps that is one of the reason why the file were not uploaded? I think you should modify your php.ini to allow larger files. Please see https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size. Or if you know how to use .htaccess, that will can also provide good support on your file uploading problem. – Itami Nov 26 '18 at 08:48
  • Yes, I have changed my data type to LongBlob. It works. – Sahil Verma Nov 26 '18 at 09:46