Create missing directories before performing ftp_put
Posted: Sat Aug 28, 2010 10:36 am
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!
Code: Select all
//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);
}