16

I have a URL where I save some projects from my work, they are mostly MDB files, but some JPG and PDF are there too.

What I need to do is to list every file from that directory (already done) and give the user the option to download it.

How is that achieved using PHP?

2
  • 1
    Isn't this handled by the browser? Commented Aug 23, 2012 at 14:39
  • 1
    but how do i get the browser to start the download? Commented Aug 23, 2012 at 14:40

4 Answers 4

45

To read directory contents you can use readdir() and use a script, in my example download.php, to download files

if ($handle = opendir('/path/to/your/dir/')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { echo "<a href='download.php?file=".$entry."'>".$entry."</a>\n"; } } closedir($handle); } 

In download.php you can force browser to send download data, and use basename() to make sure client does not pass other file name like ../config.php

$file = basename($_GET['file']); $file = '/path/to/your/dir/'.$file; if(!file_exists($file)){ // file does not exist die('file not found'); } else { header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: binary"); // read the file from disk readfile($file); } 
Sign up to request clarification or add additional context in comments.

4 Comments

I get a 403 message; checked on folder permission and is set to 777, so i have no clue what's going on.
make sure the path is ok ... path to folder and file
Check the content of your .htaccess file in the directory where your file is.
should always check that the file name is a valid filename and not including characters that should not be there. i.e. ../../../config.php might allow for downloading of the master config file of a website.
3

Here is the code that will not download courpt files

$filename = "myfile.jpg"; $file = "/uploads/images/".$filename; header('Content-type: application/octet-stream'); header("Content-Type: ".mime_content_type($file)); header("Content-Disposition: attachment; filename=".$filename); while (ob_get_level()) { ob_end_clean(); } readfile($file); 

I have included mime_content_type which will return content type of file .

To prevent from corrupt file download i have added ob_get_level() and ob_end_clean();

Comments

2

If the folder is accessible from the browser (not outside the document root of your web server), then you just need to output links to the locations of those files. If they are outside the document root, you will need to have links, buttons, whatever, that point to a PHP script that handles getting the files from their location and streaming to the response.

3 Comments

If file is image or text it will not download it.
If it's a file that opens in the browser, you can use the browser's save functionality to save it to the file system. The mission is still accomplished.
If you can tell that to customer, I've learned that most of consumers don't know what to do if they see a text file opened in browser, or a pdf file ..
2

Here is a simpler solution to list all files in a directory and to download it.

In your index.php file

<?php $dir = "./"; $allFiles = scandir($dir); $files = array_diff($allFiles, array('.', '..')); // To remove . and .. foreach($files as $file){ echo "<a href='download.php?file=".$file."'>".$file."</a><br>"; } 

The scandir() function list all files and directories inside the specified path. It works with both PHP 5 and PHP 7.

Now in the download.php

<?php $filename = basename($_GET['file']); // Specify file path. $path = ''; // '/uplods/' $download_file = $path.$filename; if(!empty($filename)){ // Check file is exists on given path. if(file_exists($download_file)) { header('Content-Disposition: attachment; filename=' . $filename); readfile($download_file); exit; } else { echo 'File does not exists on given path'; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.