Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow access to protected file through PHP

I have a set of tar.gz files inside a folder protected against http access via .htaccess. I wish to allow direct curl downloading of these files via http. I have done this to things like images by setting the header information to something like:

$file='/some/file/protected/by/htaccess.png';
header("Content-type: image/png");
readfile($file);

My question: Is there a way to provide direct access to these tar.gz files in a similar manner to the way I did it with images?

The reason I would like to do it in this way is that I would like to control access to which users can access these files by our site's login system.

Edit: as pointed out by Machavity, my code was pointing at a jpg, and had a png header.

like image 385
Csizzle Avatar asked Jan 31 '26 09:01

Csizzle


1 Answers

For tar.gz

$filename = 'path/to/your/file.tar.gz';
header('Content-Type: application/x-compressed');
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Content-Length: ' . filesize($filename));
readfile($filename);
like image 117
Tuan Anh Hoang-Vu Avatar answered Feb 02 '26 03:02

Tuan Anh Hoang-Vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!