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);
}