Download event problem in "href"

Asked

Viewed 1,538 times

2

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

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

  • 4

    And what exactly is the problem? Gives error page? File not found? Opens in browser instead of downloading?

  • I have a system in php, in it brings a table that one of the columns is several ID that when clicking on it, it downloads a file referring to the ID. The file is on a Centos Linux server

  • Do you know the file path on the server? Is it accessible to the web server? Please use the [Edit] link above to include this information in the question itself.

3 answers

1


Solution Listing Files by ID of a Table

I imagine you have a chart or a way to make one from/to file ID for its respective path (path).

Let’s assume you have the table with the fields ID and CAMINHO, thus:

ID   NOME
1    /arquivos/planilha.xls
2    /arquivos/documento.pdf

And on the page you have manage to 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 make 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 it is good for you to do some treatments, such as when the script does not receive any parameter or does not find the ID in the database.

See the job documentation readfile().

If you want to force the download of files that by default would be opened in the browser (HTML, text, image, etc.), you can also put headers, such as:

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($caminho));

Note that headers need to be set before writing the file, i.e., before calling the function readfile();.

Solution with direct link to the file

After seeing the question update, it was 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 server root.

<a href="/interno/2014/01/30/1391088944.2621-11_35_44-4002-1006.WAV">Download</a>

If you want to list the files of a folder using PHP and create the links automatically, you can use the function scandir() PHP 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.

  • there to look for the file I’m having problems too, how can I do to locate this file being that I don’t have a fixed folder?

  • @Haonenakano If you don’t have a database and the file is inside a visible server directory, as seems to be the case after seeing your update, then you just need to generate a direct link. I will update the response to reflect this.

  • @Haonenakano Updated response!

  • I’m a bit of a layman, I’d like to know what to put in the variable $dir.

0

I don’t know if I got the question right, but to search for a file on a linux server, you can use Ack/grep. Install the package, if it’s Ubuntu:

$ sudo apt-get install ack-grep

And to search the file you can use:

$ ack nome_do_arquivo.txt ~/

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

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

  • I do not know why they voted against this answer... It is the only one that really addresses the part of encontrar um arquivo no servidor that the OP asked. I will give upvote to compensate. But I agree that this answer is incomplete. The @Elvismoreira should have commented on how to run the ack nome_do_arquivo.txt ~/ from PHP, for example exec(), passthru(), etc.

0

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

You informed that the site is at /var/www/html/ligacoes, you probably access the browser by the url http://localhost/ligacoes, following your example above, the file /var/www/html/ligacoes/interno/2014/01/30/1391088944.2621-11_35_44-4002-1006.WAV 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 answered your question!

  • but when I don’t have a fixed path as in the example you passed?

  • You will need to know which is the path, being saved in the database (the whole path) or some logic, for example the files are in some specific folder and you identify them with the table id, There are countless logics and you will have to know which one is to solve your problem. But if you have multiple files and do not know how to identify them with your respective line in the bank, it is virtually impossible without a pre-defined logic!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.