Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check using PHP FTP functionality if folder exists on server or not?

Tags:

php

Is there a way to check if a a folder exists on the server using PHP Ftp functionality?

like image 206
Elitmiar Avatar asked Oct 12 '09 12:10

Elitmiar


People also ask

How to check whether file exists in php?

The file_exists() function checks whether a file or directory exists.

How do you create a file if it doesn't exist in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

What is FTP server in PHP?

PHP FTP IntroductionThe FTP functions give client access to file servers through the File Transfer Protocol (FTP). The FTP functions are used to open, login and close connections, as well as upload, download, rename, delete, and get information on files from file servers.


3 Answers

For PHP 5:

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

http://php.net/manual/en/function.is-dir.php : "As of PHP 5.0.0, this function can also be used with some URL wrappers."

http://php.net/manual/en/wrappers.ftp.php : [Support] "As of PHP 5.0.0: filesize(), filetype(), file_exists(), is_file(), and is_dir()"

like image 154
Piskvor left the building Avatar answered Oct 18 '22 22:10

Piskvor left the building


Try this:

if (ftp_nlist($ftp_stream, $new_folder) == false) {
    ftp_mkdir($ftp_stream, $new_folder);
}
like image 44
Claytinho Avatar answered Oct 18 '22 20:10

Claytinho


There is no 'exists' function for ftp connections in php.

You could try ftp_chdir and check the result

like image 6
Ewout Avatar answered Oct 18 '22 22:10

Ewout