0

how can I find out, if file exist in folder, which is password protected via .htaccess (I know password)

This not working:

$filename = "http://username:password@website.com/pictures/12.jpg"; // folder have password


if (file_exist($filename)) {

return TRUE;

} else return FALSE;

but if some other folder dont have .htacces password, this code nicely works:

$filename = "http://website.com/pictures_without_password/12.jpg"; // folder dont have password

if (file_exist($filename)) {

return TRUE;

} else return FALSE;

Thanks

1 Answers1

1

file_exists over HTTP can be finnicky,

Please try the following:

$file = 'http://username:password@website.com/pictures/';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
} else {
    $exists = true;
}

Alternatively:

function get_http_response_code($theURL) {
    $headers = get_headers($theURL);
    return substr($headers[0], 9, 3);
}

$file = 'http://username:password@website.com/pictures/';
if(get_http_response_code($file) == '404') {
    $exists = false;
} else {
    $exists = true;
}
t3chguy
  • 1,018
  • 7
  • 17
  • 1
    There's also an example on the [`get_headers()` manual page that describes how to get *just* the HTTP code](http://php.net/manual/en/function.get-headers.php#97684). (What if the server used `HTTP/1.0` or eventually `HTTP/2.0`?) – h2ooooooo Jul 18 '14 at 08:24