Solution listing files by ID of a table
I imagine you have a table or a way to make a from / to of the file ID to its path.
Let's suppose you have the table with the fields ID
and CAMINHO
, like this:
ID NOME
1 /arquivos/planilha.xls
2 /arquivos/documento.pdf
And on the page you can list the files like this:
<a href="download.php?id=1">planilha.xls</a>
<a href="download.php?id=2">documento</a>
So, in the script download.php
you need to do a logic like this:
$id_arquivo = $_GET['id'];
$caminho = recuperaCaminhoPorId($id_arquivo); //implementar esta função em algum lugar
readfile($caminho); //lê o arquivo e manda para o usuário
Obviously you should do some treatments, such as when the script does not receive any parameters or does not find the ID in the database.
See the documentation of the function readfile()
.
If you want to force the download of files that would normally be opened in the browser (HTML, text, image, etc.), you can also place headers, such as:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($caminho));
Note that headers need to be defined before writing the file, that is, before calling the readfile();
function.
Solution with direct link to file
After viewing the update of the question, it became clear that the downloadable files are in a folder available on the HTTP server.
To make the file available, you can create a link with the path relative to the root of the server.
<a href="/interno/2014/01/30/1391088944.2621-11_35_44-4002-1006.WAV">Download</a>
If you want to list the files in a folder using PHP and create the links automatically, you can use PHP's scandir()
function to list the files and make a for
to print the links.
Example:
$files = scandir($dir);
$basedir = '/var/www/html';
foreach($files as $f) {
if($f != '.' && $f != '..') {
echo '<a href="' . str_replace($basedir,'',$f) . '">'
. basename($f) . '</a>';
}
}
This is just a simple idea that you can adapt to your need.