By : house34 Published On Friday, July 25, 2014, 05:40 In PHP Scripts
//Connect and Login
$conn_id = FTP_connect("sampleserver.com");
$login_result = FTP_login($conn_id, "sampleusername", "samplepassword");
$old_dir_name = 'olddirectoryname';
$new_dir_name = 'newdirectoryname';
//First attempt to rename the file. We are assuming that the current directory is root.
if (FTP_rename($conn_id, $old_dir_name, $new_dir_name)) {
echo "successfully renamed $old_dir_name to $new_dir_name
";
} else {
echo "There was a problem while renaming $old_dir_name to $new_dir_name
";
}
//Next change the file permissions.
if (FTP_chmod($conn_id, 0777, $new_dir_name) !== false) {
echo "$file chmoded successfully to 777
";
} else {
echo "could not chmod $new_dir_name
";
}
Using the MightyFTP Api
//Include MightyFTP/FTPClient.php
require_once("MightyFTP/FTPClient.php");
function renameAndChangePermissions()
{
//Create MightyFTPFTPClient on login to sampleserver.com
$mightyFTPClient = new MightyFTPFTPClient("sampleserver.com", new MightyFTPFTPCredentials("sampleuser", "samplepassword"));
//Get the root directory
$rootDirectory = $mightyFTPClient->getRootDirectory();
$rootChildren = $rootDirectory->getChildren();
//Once you have a reference to the directory you can rename and chmod the directory in a single line of code.
$rootChildren["olddirectoryname"]->rename("newdirectoryname")->chmod(0777);
//If no exceptions have been thrown then assume that everything was successful
echo "Successfully renamed olddirectoryname to newdirectoryname and changed permissions of directory to 777";
}
try
{
renameAndChangePermissions();
}
catch(MightyFTPFTPException $ex)
{
//If an exception was thrown then print exception message.
echo $ex->message();
}