Copie y descargue el archivo en la carpeta protegida de htaccess

I have a htaccess password protected folder with several files in it. Users are not allowed to access all files, but are allowed to download their own.

Since i can't direct link the file and since copying / removing isn't a real solution, i thought i'd just open the file using file_get_contents and echo it back into the page using the right header. But.. i don't get it working.. Here is my code. The error i am getting is that when opening the file i get a "file is damaged" error from Acrobat.

$file = "cms/docs/5641-1.pdf";
header('Content-Description: File Transfer');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename='.basename("exoticfilename.pdf"));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
if (file_exists($file))
    echo file_get_contents($file);

Also, in this example I am just using a PDF file, but there are several types of files. Therefore i should probably change the header depending on the file type. Is there a solution for that, or should i just use a very long if / else statement?

If there is another, better way, I am open for that.


The above works, but not with all files. Older PDF's (Acrobat 6) don't work, but Acrobat X files do. Same counts for the docx files. Some work, others don't. Very weird, since I am able to open all directly on my PC. I assume it has something to do with the application/pdf line (or application/vnd.openxmlformats-officedocument.wordprocessingml.document' for docx). All others, like images, work.

preguntado el 31 de enero de 12 a las 08:01

I copied your code and it works fine form me. Try to add die() después de file_get_contents -

Thank you Narek. I tried die() but that didn't work. I just tested it with an older PDF and that works. Could it be that there is something wrong in the header of the file? Is there perhaps some sort of "general" header i can use for different PDF's, DocX, Tiffs and so on? I mean in this line header('Content-type: application/pdf'); -

@mat, I see your point, I'll delete my previous comments, as they seem to be out of place. -

1 Respuestas

Since you are using htaccess/htpasswd to protect the directory from hot-linking leeches. You are inadvertanly blocking access to the files from an outside source such as a browser from the client side. Since the directory requires authentication to access the files within it, you need to script around it. In a sense authenticating through the script. I have seen it done before, and you can find one of many references on the subject here

but bottom line is htaccess and htpasswd over run your scripts even if on the same host machine, as they are in a lack of better description server level, ran before even php starts its process on a page load.

Respondido el 31 de enero de 12 a las 13:01

Not entirely since I am able to download the files. Even the right files sometimes, but older PDF's seem to cause the problem. The same counts for older doc files. - estera

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.