Web Hosting Talk







View Full Version : Creating folders in FTP server


flashwebhost
08-31-2006, 05:43 PM
Hi,

I want to make a PHP script that will upload files to FTP server with different folders.

The script should create folder, if folder is not exist in FTP server.



# change the directory
if (ftp_chdir($conn_id, $remote_folder)) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {

echo "Couldn't change directory <BR>";

if (ftp_mkdir($conn_id, $remote_folder)) {
echo "successfully created $remote_folder <br>";
} else {
echo "There was a problem while creating $remote_folder <br>";
exit(0);
}

if (ftp_chdir($conn_id, $remote_folder)) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "<BR>";
} else {
echo "Couldn't change directory <BR>";
exit(0);
}

}


This is the code i use. It will try change directory, if it fails, Try creating FOLDER, then try change again. Still it do not work, script will exit.

The problem with the script is, when folder is not present, it will show error message like


Warning: ftp_chdir() [function.ftp-chdir]: Can't change directory to kerala: No such file or directory in D:\sites\test.php on line 18
Couldn't change directory
successfully created kerala
Current directory is now: /kerala


Is there any way i can avoid this error in cause of FOLDER not present ?

Anyway to check presense of folder ?

Thanks,

Yujin

horizon
08-31-2006, 05:48 PM
You might want to 'umask' the specified folder's permission first. Then, use the @mkdir ('@' is important in this case) and then set the appropriate CHMOD level value once it's created. ;)

flashwebhost
08-31-2006, 05:54 PM
I don't have a permission issue. File upload works if folder exists or even after the warning. I will use the @

umask - can you explain what it does :-)

horizon
08-31-2006, 06:02 PM
umask - can you explain what it does

Sure, see what it does from here:

http://ca.php.net/umask

;)

[edit]


Note: Avoid using this function in multithreaded webservers. It is better to change the file permissions with chmod() (http://ca.php.net/manual/en/function.chmod.php) after creating the file. Using umask() can lead to unexpected behavior of concurrently running scripts and the webserver itself because they all use the same umask.


Change of thought. It is said above to use chmod rather than umask. Use chmod instead. ;)

Lightwave
08-31-2006, 06:05 PM
Use the ftp_nlist command to see if the directory exists. If it does... skip the mkdir section.

horizon
08-31-2006, 06:06 PM
You can also use the is_dir function to see if the target directory does exist. I forgot to mention it earlier but this is what I do when I'm about to create a folder under FTP with PHP. ;)

flashwebhost
08-31-2006, 06:42 PM
Lightwave, I will try ftp_nlist.

horizon, is_dir will work with FTP or just with file system ?

horizon
08-31-2006, 06:53 PM
horizon, is_dir will work with FTP or just with file system ?

is_dir = is directory

it only checks for directory structures, not for filesystems.

If you wish to check for filesystems, you can always use:


file_exists

flashwebhost
08-31-2006, 07:30 PM
horizon, is_dir and file_exists can be used to check files and folders in local file system.

But the folder i need to check is in remote FTP server. Can is_dir can be used to check a folder exists in remote FTP server ?

Lightwave, i tried ftp_nlist, but in the result, there is no way to identify between file and folders. That is if a file with same name exists, it can be mistaken as a folder.

horizon
08-31-2006, 08:13 PM
Can is_dir can be used to check a folder exists in remote FTP server ?


is_dir module cannot be used for remote folders. In order to do so, you'd need to create a general function and then set a preg_match argument to see if the remote folder is acknowledgable. ;)