PHP MySQL PDF Upload Save to Database and Download
pdf file upload and download in php |
You can Upload any Documents Easily To Desired Folder and Save Information to Database.
Learn how to Upload large file in php
Also Learn
1. PHP MySQL Pagination
2. PHP MySQL Login with SESSION
3. php mysql comment system
4. sort MySQL table column using PHP
5. PHP MySQL Shopping Cart
##What You will Need:
##create 4 files and 1 folder named 'files' in same folder.1. connect.php
2. index.php
3. upload.php
4. download.php
5. style.css
6. files [folder name] ..code is updated please add a new file named download.php
how to upload pdf file in mysql database using php |
Database Name: test
Table name: pdf
Using The Below SQL Commands for Creating Table 'pdf'
CREATE TABLE IF NOT EXISTS `pdf` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `size` varchar(100) NOT NULL, `type` varchar(10) NOT NULL, PRIMARY KEY(id) ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Now copy The Below Codes for Certain File.
<?php // Creating connection test is our database name $conn = new mysqli('localhost', 'root', '','test'); // Checking connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
Create Index.php to Display Uploaded File with Download Link.
<?php include("connect.php"); include('upload.php'); ?> <hr/> <link rel="stylesheet" href="style.css" type="text/css"/> <table> <tr> <th>Book Name</th> <th>Size</th> <th>Type</th> <th>Action</th> </tr> <?php $result=mysqli_query($conn,"SELECT*FROM pdf ORDER by ID DESC"); while($row=$result->fetch_array()){ ?> <tr> <td><?php echo $row['name'];?></td> <td><?php echo number_format($row['size']/(1024*1024),2);?>MB</td> <td><?php echo $row['type'];?></td> <td><a href="download.php?pdf=<?php echo $row['name'];?>">Download</a></td> </tr> <?php }?> </table>
Create Upload.php file to Display Upload form and Processing Upload Selected File.
Add the below HTML codes top of the upload.php file to Display Upload form.
<h2>Upload,Save and Download PDF File Using PHP MySQL</h2> <form method="POST" action="" enctype="multipart/form-data"> <input type="file" name="pdf"/> <input type="submit" name="submit" value="Upload"/> </form> <?php include("connect.php"); $errors=1; //Targeting Folder $target="files/"; if(isset($_POST['submit'])){ $target=$target.basename($_FILES['pdf']['name']); //Getting Selected PDF Type $type=pathinfo($target,PATHINFO_EXTENSION); //Allow Certain File Format To Upload if($type!='pdf' && $type!='doc' && $type!='docx' && $type!='epub' ){ echo "Only PDF,DOC,DOCX,Epub files format are allowed to Upload"; $errors=0; } //Checking for File Size 1000000 bytes== 1MB $filesize=$_FILES['pdf']['size']; if ($filesize < 100 && $filesize< 2000000){ echo 'You Can not Upload Large File(2MB) by Default ini setting..<a href="http://www.codenair.com/2018/03/how-to-upload-large-file-in-php.html">How to upload large file in php</a>'; $errors=0; } //checking for Exsisting Doc File Files if(file_exists($target)){ echo "File Already Exist"; $errors=0; } // Check if $errors is set to 0 by an error if($errors==0){ echo 'Your File Not Uploaded'; }else{ //if not error encountered //Moving The PDF or Doc file to Desired Directory $uplaod_success=move_uploaded_file($_FILES['pdf']['tmp_name'],$target); if($uplaod_success==TRUE){ //Getting Selected PDF Information $name=$_FILES['pdf']['name']; $size=$_FILES['pdf']['size']; $result=mysqli_query($conn,"INSERT INTO pdf (name,size,type)VALUES('$name','$size','$type')"); if($result==TRUE){ echo "Your pdf '$name' Successfully Upload and Information Saved Our Database"; } } } } ?>
download.php
In this step we order to the browser to download file forcefully..
<?php if(isset($_GET['pdf'])) { $var_1 = $_GET['pdf']; $dir = "files/"; // trailing slash is important $file = $dir . $var_1; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } } ?>
Now add some CSS codes to Style our Index Page.
table th, td{ border: 1px solid green; text-align:left; } th,td{ padding:8px; } table{ border-collapse:collapse; width:50%; } table td a{ background-color: #4CAF50; border: none; color: white; padding: 7px 12px; text-decoration: none; margin: 4px 2px; cursor: pointer; } th{ padding:15px; background:lightgreen; font-size:20px; font-weight:bold; } input[type=file]{ padding:5px 0px 5px 5px; background:black; border-radius:5px; } input[type=submit]{ background:black; color:white; padding:6px 8px; font-weight:bold; cursor:pointer; border-radius:5px; }
Ok Now You have Successfully Created PHP MySQL Pdf upload and Display Script.Please Visit Again for More Codes.
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.