• We just launched and are currently in beta. Join us as we build and grow the community.

How to Download File using PHP/MySQLi

a7961

Cyber Evidence Collector
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
57
Likes
89
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In my previous tutorial, I've created a Simple File Upload using PHP/MySQLi. As a follow-up, I've created another tutorial on how to create a Simple File Download. Most files can be easily download by clicking their link. However, some files cannot be downloaded by doing so. Thus, I've created this tutorial. This tutorial will not give you a good design, but will give you a good idea about the topic.

Creating our Database

First, we're going to create our database. This contains the location of our files.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "download".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE

    TABLE

    `file`

    (
  2. `fileid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `file_location`

    VARCHAR

    (

    150

    )

    NOT

    NULL

    ,
  4. PRIMARY

    KEY

    (

    `fileid`

    )
  5. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_3.png

Inserting Data in to our Database

Next step is to insert example data into our database. This will serve as our sample data.
1. Click "dowload" database that we have created earlier.
2. Click SQL and paste the code below.

  1. INSERT

    INTO

    `file`

    (

    `file_location`

    )

    VALUES
  2. (

    'upload/butterfly.jpeg'

    )

    ,
  3. (

    'upload/cherry.jpeg'

    )

    ,
  4. (

    'upload/rose.jpeg'

    )

    ;

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php

  2. //MySQLi Procedural
  3. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "download"

    )

    ;
  4. if

    (

    !

    $conn

    )

    {
  5. die

    (

    "Connection failed: "

    .

    mysqli_connect_error

    (

    )

    )

    ;
  6. }

  7. ?>

Creating our File Source

I've included in the file of this tutorial, the folder named "upload" which contains our sample files.

Creating our Table

Next is to create our sample table. This contains the data that we inserted earlier and our download link. We name this as "index.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>How to Download File using PHP/MySQLi</title>
  5. </head>
  6. <body>
  7. <h2>Sample Images</h2>
  8. <table border="1">
  9. <thead>
  10. <th>File Location</th>
  11. <th>Action</th>
  12. </thead>
  13. <tbody>
  14. <?php
  15. include

    (

    'conn.php'

    )

    ;

  16. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from file"

    )

    ;
  17. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  18. ?>
  19. <tr>
  20. <td><?php

    echo

    $row

    [

    'file_location'

    ]

    ;

    ?>

    </td>
  21. <td><a href="download.php?file=<?php

    echo

    urlencode

    (

    $row

    [

    'file_location'

    ]

    )

    ;

    ?>

    ">Download</a></td>
  22. </tr>
  23. <?php
  24. }
  25. ?>
  26. </tbody>
  27. </table>
  28. </body>
  29. </html>

Creating our Download Code

Lastly, We create our download code. This code will download the file when user clicks the download link of that file. We name this as "download.php".

  1. <?php

  2. $filePath

    =

    urldecode

    (

    $_REQUEST

    [

    'file'

    ]

    )

    ;

  3. if

    (

    file_exists

    (

    $filePath

    )

    )

    {
  4. $fileName

    =

    basename

    (

    $filePath

    )

    ;
  5. $fileSize

    =

    filesize

    (

    $filePath

    )

    ;

  6. header

    (

    "Cache-Control: private"

    )

    ;
  7. header

    (

    "Content-Type: application/stream"

    )

    ;
  8. header

    (

    "Content-Length: "

    .

    $fileSize

    )

    ;
  9. header

    (

    "Content-Disposition: attachment; filename="

    .

    $fileName

    )

    ;

  10. readfile

    (

    $filePath

    )

    ;
  11. exit

    (

    )

    ;
  12. }
  13. else

    {
  14. die

    (

    'The provided file path is not valid.'

    )

    ;
  15. }
  16. ?>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top