Home > PHP > File Operations With FTP

File Operations With FTP

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 file permission/attribute, and rename) by web masters. In this topic, we will focus on the file 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 file
//Create a temporary file
$tmp = tmpfile();
//Upload the created temporary file
//We will create a new test.txt file in public_html directory
@ftp_fput($ftp, ‘test.txt’, $tmp, FTP_ASCII);

//Change the file permission/attribute
//eg: We change the permission/attribute of test.txt
ftp_site($ftp,"CHMOD 0644 test.txt");
//the code 0644 is a file attribute code
//For file, the second digit indicate the right for the file owner
//The third digit indicate the right for the file owner group
//The last digit indicate the right for other users
//6 indicate full access (read and write permission)
//4 indicate limited access (read only permission)
//0 indicate restriced access (no permission)
//0644 means the owner can read and write the file, and the file
//owner group and other users only can read the file

//Modifying the file content
//eg: We modify the content of test.txt
//First, we should make the file writable by all
@ftp_site($ftp,"CHMOD 0666 test.txt");
//Write to file, because we use the fopen function, we have to provide a
//complete directory path or relative directory path to file
$fp = @fopen(’home/root/public_html/test.txt’, ‘w’);
@fputs($fp,’This file has been modified’);
@fclose($fp);
//Make the file writable only to owner
@ftp_site($ftp,"CHMOD 0644 test.txt");

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

//Rename a file
//eg: We rename test.txt to test_renamed.txt
if(@ftp_rename($ftp, ‘test.txt’, test_renamed.txt))
echo ‘File renamed’;
else
echo ‘Cannot rename the file. File may be deleted or you are not allowed
to rename this file’;

//Move a file to another directory
//eg: We move test.txt to test folder
if(@ftp_site($ftp,"mv test.txt test/");)
echo ‘File has been moved’;
else
echo ‘Cannot move the file. File may be deleted or you are not allowed
to perform this operation’;

//Copy a file to another directory
//eg: We copy test.txt to test folder
if(@ftp_site($ftp,"cp test.txt test/");)
echo ‘File has been copied’;
else
echo ‘Cannot copy the file. File may be deleted or you are not allowed
to perform this operation’;
}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
PHP: fopen 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.