Hansi Zimmrer
Hansi Zimmrer

Reputation: 259

Check if path on a remote server exists

Do you know how to manage that? I know that one can specify a context using the mkdir function, but using this function I cannot check if the path is existing, can't I?

EDIT:

FTP connection to the server is established.

Upvotes: 0

Views: 2288

Answers (2)

hek2mgl
hek2mgl

Reputation: 158220

You can use is_dir() together with the FTP protocol. You can test it:

var_dump(is_dir('ftp://ftp.debian.org/debian/')); // bool(true)

However, you cannot do the same with the HTTP protocol:

var_dump(is_dir('http://ftp.debian.org/debian/')); // bool(false)

This is because the HTTP protocol does not provide information about whether the remote resource identifies a folder or a file.


Update: Is it not fully true that the HTTP protocol does not know about whether the remote resource is a directory or not. There is a special mimetype definition for that, which the server could send along with the Content-Type header, which could be checked by clients:

Content-Type: httpd/unix-directory

But it is rarely used in the wild. Most web servers disable directory listings at all for security reasons.

Upvotes: 1

GuyT
GuyT

Reputation: 4426

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

if (file_exists('path/to/file/or/dir')){ doSomething(); } else { createFileOrDir(); }

For a remote server use:

is_dir('ftp://user:[email protected]/some/dir/path');

Upvotes: 1

Related Questions