-1

PHP Beginner. File uploading is successful but my browser doesn't download the files, instead it reads the file. So i referred other threads and found below code which is not working. I want to download files when i click on the hyperlink download. Selected the path from MySQL database.

$rows = mysqli_num_rows($result);

                if($rows>0)
                {

                    while($row = mysqli_fetch_assoc($result))
                    {   
                        ?>
                        <div> <?php echo $row['Object_Name'];?> 
                        <a href="<?php 
                        $file_url = $row['Object_Path'];
                        header('Content-Type: application/octet-stream');
                        header("Content-disposition: attachment; filename=\"".$row['Object_Name']. "\""); 
                        readfile($file_url);
                        ?>">Download</a><br>
                        </div>  
                        <?php
                    }

                }
Sudin
  • 45
  • 1
  • 9
  • I didn't understand your question. Are you asking "How to write code (using php) that will allow users to download a file from your website by clicking a link?". – Webeng Apr 19 '16 at 22:27
  • 2
    You can not do in this way. When you output a file, you can output **only** the file. No `
    `, no HTML, no any other text.
    – fusion3k Apr 19 '16 at 22:28
  • @Webeng yes, how can i do it? – Sudin Apr 19 '16 at 22:33
  • @fusion3k I dint get you exactly. I want to list all the files with download option, so I thought this is the right way. – Sudin Apr 19 '16 at 22:36
  • 1
    Possible duplicate of [How to force file download with PHP](http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php) – miken32 Apr 19 '16 at 23:05
  • @miken32 That dint work out. And its different... – Sudin Apr 19 '16 at 23:16

2 Answers2

2

In a paged called download.php, have the following code:

<?php

$filename = 'file.pdf';//this should be the name of the file you want to download 
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers 
header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));

readfile($filename);

exit;
?>

Your main page should then have a link to the download page like this:

<a href="download.php">DOWNLOAD</a>

Let me know if that works for you.


Edited:

My previous example was for the download of a pdf file. In the case that you want to download a different type of file, a few lines have to be slightly modified. I recommend you first try downloading a pdf file with the previous code, and after having accomplished that testing out on other files.

To retrieve the path from the database, you can use MySQL (PDO).

$sqlStatement = "SELECT path FROM my_table WHERE some_id = ".$something;
/*if you are retrieving the path from the database, 
you probably have a lot of different paths available 
there, so only you know the criteria which will decide
which of the many paths it is that you choose to extract*/

$sqlPrepared = $connection->prepare($sqlStatement);
$sqlPrepared->execute();

$row_info = fetch($sqlPrepared);

$filename = $row_info['path'];// this would be the $filename = 'file.pdf' 
//that was in the example above

If you are not sure how to connect to the database, there are a lot of articles online explaining MySQL that is relatively straightforward.

I hope that helped :)

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • The path is stored in the database. How can i pass that to the download.php ? I mean is it an appropriate way to use POST and GET – Sudin Apr 19 '16 at 22:50
  • @Sudi I edited my answer in my response, and I'm not sure what POST and GET have to do with this particular situation? If you mean that different links will give you different downloads, then you can use `$_GET['id']` as the $something value in the `$sqlStatement` above to choose which path you want from your database, but make sure you use `htmlspecialchars()` to prevent sql injection attacks: `$something = htmlspecialchars($_GET['id'])` – Webeng Apr 19 '16 at 23:00
  • It works!!!!! Thank a lot man. But one problem, it is downloading every files extension as .pdf . How can i keep the original extension name? – Sudin Apr 19 '16 at 23:11
  • @Sudi The previous code had `header('Content-Type: application/pdf');` in it. Depending on the type of file you want to download, this line must be changed to fit the type of file you want. Some examples of different options can be found in the php documentation: http://php.net/manual/en/function.header.php . If this answer has answered your question, don't forget to accept the answer with a green check mark :) – Webeng Apr 19 '16 at 23:16
0

You have to use two separate files.

In link page, you can output a HTML like this:

<a href="http://www.example.com/download.php?file=1">Download file 1</a>
<a href="http://www.example.com/download.php?file=2">Download file 2</a>
<a href="http://www.example.com/download.php?file=3">Download file 3</a>
(...)

You can use a <form>, if you prefer.

Then, in download.php:

  1. Select appropriate file using GET/POST parameter ($_GET['file'] in above example);

  2. send appropriate headers (like in your original code);

  3. echo your file (you can use readfile);

  4. Mandatory: no additional output in this script! Even a single additional space will corrupt downloaded file.

fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • Thank for helping. I followed your trick and the headers provided by Webeng. Worked great... – Sudin Apr 19 '16 at 23:14