Home > PHP > Directory Operations With FTP

Directory Operations With FTP

As explained in our previous article, FTP, stands for File Transfer Protocol, has been widely used for file and directory/folder operations (such as: create, copy, delete, move, change working directory, change directory permission/attribute, and rename) by web masters. In this topic, we will focus on the directory operations using PHP FTP.

Source Code


[php]<?
//By : Isusx Programming Corner
//URL : http://isusx.com

//Specify your FTP server (Can be the server name or IP Address)
$ftp_server = ‘localhost’;
//Specify your FTP port (Default FTP port is 21
$ftp_port = 21;
//Specify your FTP account
$ftp_user = ‘username’;
$ftp_pass = ‘password’;

//Set up a connection to FTP server
$ftp = @ftp_connect($ftp_server, $ftp_port);

if($ftp){
//Connected to FTP Server
//FTP user authentication
$auth = @ftp_login($ftp, $ftp_user, $ftp_pass);
if($auth) {
//Authenticated

//Change to your working directory (ex: our working directory is public_html)
if (@ftp_chdir($ftp, "public_html")){
//Create a new directory
//We will create a new test directory in public_html directory
if(@ftp_mkdir($ftp, ‘test’))
echo ‘Directory created’;
else
echo ‘Cannot create the directory. Directory may be exist or you are not
allowed to create a directory’;

//Change the directory permission/attribute
//eg: We change the permission/attribute of test directory
ftp_site($ftp,"CHMOD 0755 test");
//the code 0755 is a directory attribute code
//For directory, the second digit indicate the right for the directory owner
//The third digit indicate the right for the directory owner group
//The last digit indicate the right for other users
//7 indicate full access (can perform all actions to directory)
//5 indicate limited access (can only list directory content)
//0 indicate restriced access (no permission)
//0755 means the owner have full control of that directory, and the directory
//owner group and other users only can list the directory content

//Delete a directory
//eg: We delete test directory
if(@ftp_rmdir($ftp,’test’))
echo ‘Directory deleted’;
else
echo ‘Cannot delete the directory. Directory may be deleted or you are not
allowed to delete this directory’;

//Rename a directory
//eg: We rename test to test_renamed
if(@ftp_rename($ftp, ‘test’, ‘test_renamed’))
echo ‘Directory renamed’;
else
echo ‘Cannot rename the directory. Directory may be deleted or you are not
allowed to rename this directory’;
}else
die "Directory not found";
}else{
//Not authenticated
echo ‘Wrong username or password’;
}
//Close FTP connection
ftp_close($ftp);
}else{
//Failed to connect to FTP server
//May be caused by wrong port, network connection, or invalid server
echo ‘Cannot connect to FTP server’;
}
?>[/php]

Copyright Note

This script is free to use and can be modified as your wish.

Related Links
About FTP
PHP: FTP Manual

If you like this code/script, perhaps you would kindly treat me a drink. Any number is welcome. Thanks :)

Related Article

  1. No comments yet.
  1. No trackbacks yet.