Reputation: 169
Is there anyway to check if a file, in my case an image file, is behind htaccess?
I am trying to echo out the path to an image src where in some cases the file will be behind an htaccess required authentication. In that case I want it to display nothing. Right now, it presents the login/password prompt because I am referencing that image.
Sample:
folder2 is behind required authentication
$folders = ['folder1','folder2'];
foreach($folders as $folder){
if(is_readable($folder.'/thumb.png'))
echo "<img src='{$folder}/thumb.png' />";
else
echo 'img not found';
}
I have tried is_readable and file_exists, both seem to still present the prompt.
edit: htaccess file
AuthType Basic
AuthUserFile /home/.../.htpasswd
AuthName "Please Log In"
require valid-user
Upvotes: 1
Views: 112
Reputation: 503
Using something like cURL, you could send an HTTP request and see the request code. If it shows 401 or 403, it's unauthorized. Otherwise, it's fine.
UPDATE: It would be simpler just to use the get_headers()
method for this. The first item in the array will be the response code, which you can handle as outlined above.
Upvotes: 4
Reputation: 1615
You can read the .htaccess files and look for the require word, or do something smarter depending on how your .htaccess files might vary:
Something like this:
if(strpos(file_get_contents($folder."/.htaccess"), "require") === false)
{
// show image
}
Of course you'll want to cache the output for each folder.
Upvotes: 1