Results 1 to 10 of 10
  1. #1

    Creating folders in FTP server

    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

  2. #2
    Join Date
    Mar 2006
    Posts
    965
    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.

  3. #3
    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 :-)

  4. #4
    Join Date
    Mar 2006
    Posts
    965
    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() 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.

  5. #5
    Join Date
    Apr 2003
    Location
    San Jose, CA.
    Posts
    1,622
    Use the ftp_nlist command to see if the directory exists. If it does... skip the mkdir section.

  6. #6
    Join Date
    Mar 2006
    Posts
    965
    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.

  7. #7
    Lightwave, I will try ftp_nlist.

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

  8. #8
    Join Date
    Mar 2006
    Posts
    965
    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:

    PHP Code:
    file_exists 

  9. #9
    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.

  10. #10
    Join Date
    Mar 2006
    Posts
    965
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •