matt
matt

Reputation: 44403

php: check if path exists?

I am setting a path variable with a query-string.

What is the easiest way to check if the path (always a directory) exists or not.

    if(isset($_GET['p'])) {
    define(PATH, $_GET['p']);

So now I have like mydomain.com?p=files/folder/sub and everything works fine, i'm reading the contents of the folder. However, I can pass along ?p=shit/whatever and i don't get a 404 or anything like that. the system reads a folder which does not even exist.

I don't even need a 404, but just want to print('does not exist!') or anything similar.

What is the best method to do that?

Upvotes: 4

Views: 11240

Answers (2)

Robert Greiner
Robert Greiner

Reputation: 29762

If this is on your local machine you can use file_exists()

http://php.net/manual/en/function.file-exists.php

if (!file_exists($filename)) {
  //print your error
}

Upvotes: 1

Related Questions