Upload Pdf Files to the Database Using Pdo - Php

Hi! In this tutorial permit me show you near upload, view and download file in php and mysql. The file uploading process is similar to what we accept discussed here, only this php script not just uploads file to the server but also stores the file path and its created engagement in mysql database. Apart from uploading file, it also gives you the option to view file on browser and download it from server.

With PHP you can practically upload any type of files and the file uploading script I have shared below volition work for all file types like PDF, Certificate, Images, MP3, Videos, Zip archives etc. Just include those file extensions in the filtering process ($immune assortment) and you lot will be able to upload them.

How to Upload, View & Download File in PHP & MySQL?

Let's move on to the coding function. First you should create mysql database to store file details.

Create MySQL Database:

CREATE DATABASE `demo` ; Use `demo`; CREATE Table IF Non EXISTS `tbl_files` (   `id` int(9) NOT NULL AUTO_INCREMENT,   `filename` varchar(255) NOT NULL,   `created` datetime NOT NULL,   PRIMARY KEY (`id`) ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;        

Next is the database connectivity script that establishes connection to mysql database from php.

dbconnect.php

<?php //connect mysql database $host = "localhost"; $user = "username"; $laissez passer = "password"; $db = "demo"; $con = mysqli_connect($host, $user, $pass, $db) or die("Fault " . mysqli_error($con)); ?>        

Then create alphabetize.php - this is the main file containing user interface. It has an upload course and a html tabular array to display the list of uploaded files from database along with 'View' & 'Download' links for them.

index.php

<?php include_once 'dbconnect.php';  // fetch files $sql = "select filename from tbl_files"; $upshot = mysqli_query($con, $sql); ?>  <!DOCTYPE html> <html> <head>     <title>Upload View & Download file in PHP and MySQL | Demo</title>     <meta content="width=device-width, initial-calibration=one.0" name="viewport" >     <link rel="stylesheet" href="css/bootstrap.css" blazon="text/css" /> </head> <trunk> <br/> <div grade="container">     <div class="row">         <div class="col-xs-8 col-xs-commencement-2 well">         <form action="upload.php" method="postal service" enctype="multipart/form-information">             <legend>Select File to Upload:</legend>             <div grade="form-group">                 <input type="file" name="file1" />             </div>             <div class="form-group">                 <input blazon="submit" proper noun="submit" value="Upload" form="btn btn-info"/>             </div>             <?php if(isset($_GET['st'])) { ?>                 <div class="warning alert-danger text-middle">                 <?php if ($_GET['st'] == 'success') {                         echo "File Uploaded Successfully!";                     }                     else                     {                         repeat 'Invalid File Extension!';                     } ?>                 </div>             <?php } ?>         </form>         </div>     </div>          <div grade="row">         <div class="col-xs-8 col-xs-offset-2">             <table class="tabular array table-striped table-hover">                 <thead>                     <tr>                         <thursday>#</th>                         <th>File Name</th>                         <th>View</thursday>                         <th>Download</th>                     </tr>                 </thead>                 <tbody>                 <?php                 $i = ane;                 while($row = mysqli_fetch_array($issue)) { ?>                 <tr>                     <td><?php repeat $i++; ?></td>                     <td><?php echo $row['filename']; ?></td>                     <td><a href="uploads/<?php echo $row['filename']; ?>" target="_blank">View</a></td>                     <td><a href="uploads/<?php echo $row['filename']; ?>" download>Download</td>                 </tr>                 <?php } ?>                 </tbody>             </table>         </div>     </div> </div> </body> </html>        

Notation: This demo uses twitter bootstrap for css stylesheet.

Running alphabetize.php will generate a folio with upload form and table with files details similar to this. Users tin either click on 'View' link to view the files on browser or on 'Download' to download the files from server.

upload-and-view-file-php-mysql

Finally there is 'uploads.php' file which will be executed when the form is submitted to upload the selected file. Here is where we actually upload the file to the server from the customer auto and relieve its name and uploaded date into the database.

uploads.php

<?php //cheque if course is submitted if (isset($_POST['submit'])) {     $filename = $_FILES['file1']['proper name'];      //upload file     if($filename != '')     {         $ext = pathinfo($filename, PATHINFO_EXTENSION);         $allowed = ['pdf', 'txt', 'doc', 'docx', 'png', 'jpg', 'jpeg',  'gif'];              //bank check if file type is valid         if (in_array($ext, $allowed))         {             // get concluding tape id             $sql = 'select max(id) as id from tbl_files';             $upshot = mysqli_query($con, $sql);             if (count($result) > 0)             {                 $row = mysqli_fetch_array($result);                 $filename = ($row['id']+1) . '-' . $filename;             }             else                 $filename = '1' . '-' . $filename;              //set up target directory             $path = 'uploads/';                              $created = @date('Y-m-d H:i:s');             move_uploaded_file($_FILES['file1']['tmp_name'],($path . $filename));                          // insert file details into database             $sql = "INSERT INTO tbl_files(filename, created) VALUES('$filename', '$created')";             mysqli_query($con, $sql);             header("Location: index.php?st=success");         }         else         {             header("Location: index.php?st=error");         }     }     else         header("Location: index.php"); } ?>        

This script upload file from local machine to server and stores its details into database and redirects to index.php. If everything goes correct you will be able to encounter success bulletin on completion.

upload-file-in-php-mysql-and-store-in-database

If there'due south whatever mistake you volition be notified about it.

file-upload-and-download-in-php-mysql

Also Read:

  • How to Download File from URL with PHP & Ringlet
  • PHP User Login and Signup Arrangement using MySQL

And then nosotros have seen about file upload and to view and download them using php and mysql database. If you want you tin can fix the file size limit or restrict users to upload but pdf or images etc.

lindtthemply.blogspot.com

Source: https://www.kodingmadesimple.com/2016/09/file-upload-view-and-download-php-mysql.html

0 Response to "Upload Pdf Files to the Database Using Pdo - Php"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel