File Upload and Download Using PHP and MySQLi

2
1183

This Time, I will tackle how to make File Upload and Download using PHP. In here, you will learn the easy way of uploading and downloading files with its format such as .pdf, .txt, .docx, etc.

Follow the step by step procedure on how to make File Upload and Download using PHP. See it below.

Lets Begin:

First, create a database in the MySQL and name it “dbupload“.

Second, create a table in the database that you have created.

CREATE TABLE dbupload.tblfiles (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
FileName VARCHAR( 30 ) NOT NULL ,
Location VARCHAR( 90 ) NOT NULL
) ENGINE = INNODB;

Third, create a landing page and name it “index.php“.

Fourth, create CSS Style of the page.

  1. <style type="text/css">
  2. .form{
  3. width: 100%;
  4. display: inline-block;
  5. position: inherit;
  6. padding: 6px;
  7. }
  8.  
  9. .label {
  10. padding: 10px;
  11. width: 10%;
  12. }
  13. .input{
  14. position: inherit;
  15. padding: 3px;
  16. margin-left: 2.3%;
  17. }
  18.  
  19. .btn{
  20. margin-left: 6.5%;
  21. }
  22. </style>

Fifth, do the following code for uploading the file.

  1. <?php
  2. $con = mysqli_connect("localhost","root","","dbupload");
  3. echo "Unable to connect to MySQL! ". mysqli_connect_error();
  4. }
  5. if (isset($_POST['save'])) {
  6. $target_dir = "Uploaded_Files/";
  7. $target_file = $target_dir . date("dmYhis") . basename($_FILES["file"]["name"]);
  8. $uploadOk = 1;
  9. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  10.  
  11. if($imageFileType != "jpg" || $imageFileType != "png" || $imageFileType != "jpeg" || $imageFileType != "gif" ) {
  12. if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
  13. $files = date("dmYhis") . basename($_FILES["file"]["name"]);
  14. }else{
  15. echo "Error Uploading File";
  16. }
  17. }else{
  18. echo "File Not Supported";
  19. }
  20. $filename = $_POST['filename'];
  21. $location = "Uploaded_Files/" . $files;
  22. $sqli = "INSERT INTO <code>tblfiles</code> (<code>FileName</code>, <code>Location</code>) VALUES ('{$filename}','{$location}')";
  23. $result = mysqli_query($con,$sqli);
  24. if ($result) {
  25. echo "File has been uploaded";
  26. };
  27. }
  28. ?&gt;
  29.  
  30. &lt;h1&gt;Upload and Download&lt;/h1&gt;
  31. &lt;form class="form" method="post" action="" enctype="multipart/form-data"&gt;
  32. &lt;label class="label"&gt;Filename:&lt;/label&gt;
  33. &lt;input type="text" name="filename" &gt; &lt;br/&gt;
  34. &lt;label class="label"&gt;File:&lt;/label&gt;
  35. &lt;input type="file" name="file" class="input"&gt; &lt;br/&gt;
  36. &lt;button type="submit" name="save" class="btn"&gt;&lt;i class="fa fa-upload fw-fa"&gt;&lt;/i&gt; Upload&lt;/button&gt;
  37. &lt;/form&gt;
  38.  
  39. &nbsp;

Sixth, do the following code for downloading the file.

  1. &lt;table&gt;
  2. &lt;tr&gt;
  3. &lt;td&gt;FileName&lt;/td&gt;
  4. &lt;td&gt;Download&lt;/td&gt;
  5. &lt;/tr&gt;
  6. &lt;?php
  7. $sqli = "SELECT * FROM <code>tblfiles</code>";
  8. $res = mysqli_query($con, $sqli);
  9. while ($row = mysqli_fetch_array($res)) {
  10. echo '&lt;tr&gt;';
  11. echo '&lt;td&gt;'.$row['FileName'].'&lt;/td&gt;';
  12. echo '&lt;td&gt;&lt;a href="'.$row['Location'].'"&gt;Download&lt;/a&gt;&lt;/td&gt;';
  13. echo '&lt;/tr&gt;';
  14. }
  15. ?&gt;
  16.  
  17. &lt;/table&gt;

 

 

If you have any questions or suggestion about File Upload and Download Using PHP, please feel free to contact us at our contact page.You can subscribe this site to see more of my tutorials.

Facebook Comments

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here