Daniel Florido
Daniel Florido

Reputation: 79

check if php file exists on server

I'm looking for a way to see if a file exists on the local server. I would usually use if function exists but in this case it's not an option. My question is how can I get number 2 to return true.

1.Returns true:

$acf_funcs = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/vac3/acf/page_meta/functions.php';
var_dump(file_exists($acf_funcs));

2.Returns false:

$acf_funcs = 'http://vac3:8888/wp-content/themes/vac3/acf/page_meta/functions.php';
var_dump(file_exists($acf_funcs));

Upvotes: 1

Views: 4074

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78974

To check the local server filesystem, you need to get the path component from the URL:

var_dump(file_exists($_SERVER['DOCUMENT_ROOT'] . parse_url($acf_funcs, PHP_URL_PATH)));

In the above:

parse_url($acf_funcs, PHP_URL_PATH)

Returns: /wp-content/themes/vac3/acf/page_meta/functions.php and pre-pending $_SERVER['DOCUMENT_ROOT'] yields the same as your first example.

This will NOT however check if the file is available via http://vac3:8888.

Upvotes: 2

Zak
Zak

Reputation: 7515

You can't use file_exists -- You'll need to use get_headers

$headers = get_headers('http://vac3:8888/wp-content/themes/vac3/acf/page_meta/functions.php', 1);
$file_found = stristr($headers[0], '200');

Upvotes: 4

Related Questions