generate link to download file in PHP

0

I have a page where I want to generate download links with labels these labels are generated in a for and I want to click on the label you can download for example file 1, then on the next generated label you can download the file 2 and so on I already have the name of the files and their routes, but I do not know how to download the files

In this image you can see that I generate the problem number and in the link I want to leave the option to download the file this is the code I use to generate the links but it does not work because when I enter the page, the window opens to download only the first example without clicking on the link, I have the files in a folder called uploads:

for ($i=1; $i <=count($valores) ; $i++) { 
    echo "<h4>problema ".key($valores)."</h4>";
    echo "<p>Intentos realizados ".$valores[$i]."</p>";
    $nombre=''.key($valores).'_'.$_SESSION["usuario"][0]["matricula"].'.c';
    $ruta='subidas/'.key($valores).'_'.$_SESSION["usuario"][0]["matricula"].'.c';
    //header ("Content-Disposition: attachment; filename=".$nombre); 
    //header ("Content-Type: text/plain"); 
    //header ("Content-Length: ".filesize($nombre)); 
    //readfile($nombre); 
    //echo "<a href='cuenta.php'>descargar ejercicio</a>"; 
    next($valores);
}
    
asked by Yept 29.01.2018 в 20:37
source

1 answer

1

If you want to generate a valid URL, the header are not necessary. What you have is that you take care to build the URL well.

For example:

http://www.tudominio.com/subidas/archivo.extension

Of course, you must enter correct data, or you will have a 404 error.

You can try like this.

for ($i=1; $i <=count($valores) ; $i++) { 
    echo "<h4>problema ".key($valores)."</h4>";
    echo "<p>Intentos realizados ".$valores[$i]."</p>";
    $nombre=''.key($valores).'_'.$_SESSION["usuario"][0]["matricula"].'.c';
    $ruta='http://www.tudominio.org/subidas/'.key($valores).'_'.$_SESSION["usuario"][0]["matricula"].'.c';
    $URL='<a href="'.$ruta.'">'.$nombre.'</a>';
    echo $URL;
}
    
answered by 30.01.2018 / 02:02
source