Hello all,
I wanted to allow my users to download files from my server. Usually people point a link to the file and the browser does the rest, right? The problem is that if we're talking about a image, the browser will open it and not download it.
I looked arounf the web for answers and i've found how to make the browsers download the files. The problem is that the file seems to be correctly downloaded but it's not.
Image that you have just downloaded a jpg file. The size of the file is the same as in the server but when you try to open it, the file is empty 😕
I mean, if we check it's properties, the file says that has got say 60 kb but when you try to open it, it doesn't have no image at all. The same aplies to all the other type of file.
Here is the code:
function download($ficheiro, $caminho)
{
$caminho_completo = "$caminho$ficheiro";
if(is_file($caminho_completo))
{
$tamanho_ficheiro = filesize($caminho_completo);
header("Content-type: application/x-download");
header("Content-Disposition: attachment; filename=". basename($ficheiro));
header("Content-Length:". $tamanho_ficheiro);
readfile("$caminho_completo");
return("ok");
}
else
{
return("File not found");
}
}
Can someone help please?
Jorge Ferreira