Problem in download event in "href"

2

I have a problem where I need to find a file on the server Linux with the value name of <a href> and download it when clicking on the link.

The file path is /var/www/html/ligacoes (there are several folders and there are files in the folders, for example /var/www/html/ligacoes/interno/2014/01/30/1391088944.2621-11_35_44-4002-1006.WAV

    
asked by anonymous 30.01.2014 / 15:04

3 answers

1

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.

    
30.01.2014 / 15:48
0

I do not know if I understand the right question, but to get a file on a linux server, you can use ack / grep. Install the package, if it is ubuntu:

$ sudo apt-get install ack-grep

And to search the file you can use:

$ ack nome_do_arquivo.txt ~/

The second parameter "~ /" is the directory from where you want to search.

And to create a link to download this file, you need to put it in a folder that has web access permissions. A simple example would be the Apache www folder.

    
30.01.2014 / 15:12
0

For a file to be accessed through a link, you need to send the correct path to it, see example.

You have reported that the site is in /var/www/html/ligacoes , you probably access the browser by http://localhost/ligacoes , following its example above, the /var/www/html/ligacoes/interno/2014/01/30/1391088944.2621-11_35_44-4002-1006.WAV file can be accessed in two ways:

<!-- Link Relativo -->
<a href="/interno/2014/01/30/1391088944.2621-11_35_44-4002-1006.WAV">Baixar</a>
<!-- Link Absoluto -->
<a href="http://localhost/ligacoes/interno/2014/01/30/1391088944.2621-11_35_44-4002-1006.WAV">Baixar</a>

I hope I have answered your question!

    
30.01.2014 / 16:58