PHP Developers Network

Create missing directories before performing ftp_put
Page 1 of 1

Author:  TipPro [ Sat Aug 28, 2010 10:36 am ]
Post subject:  Create missing directories before performing ftp_put

This is a function I wrote to create directories that do not exist before I perform a ftp_put. Is this already built into PHP or is there a better way of going about this? Thanks!

Syntax: [ Download ] [ Hide ]
//example usage...
$file = "temp123/temp456/temp789/myfile.txt";
checkforAndMakeDirs($ftp_con, $file);
ftp_put($ftp_con, $file);

function checkForAndMakeDirs($connection, $file) {
        $origin = ftp_pwd($connection);
        $parts = explode("/", dirname($file);

        foreach ($parts as $curDir) {
            // Attempt to change directory, suppress errors
            if (@ftp_chdir($connection, $curDir) === false) {
                ftp_mkdir($connection, $curDir); //directory doesn't exist - so make it
                ftp_chdir($connection, $curDir); //go into the new directory

        //go back to the origin directory
        ftp_chdir($connection, $origin);

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group