Skip to content Skip to sidebar Skip to footer

Files transfer via FTP using PHP

Reading Time: 2 minutes

How to connect to the web server,upload files and download files,create a directory, delete a directory,get list of the directory via FTP using php.You can transfer any type of files between server and your pc.If you don’t know what are the files exist in the server,run php script to get file names and run the following php script to download files list.See the following example and then you can understand this better.

1-Connect to the server via FTP

	$ftp_server	=	'ftp_server_name';
	$ftp_user	=	'ftp_user_name';
	$ftp_pass	=	'ftp_user_password';
//***************  CONNECT TO THE SERVER ***************************
	// set up a connection
	$ftp_connection = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
	// try to login
	if (@ftp_login($ftp_connection, $ftp_user, $ftp_pass)) {
		echo "Connected as $ftp_user, to @$ftp_server
"; } else { echo "Couldn't connect as $ftp_user
"; }

2-Upload files via FTP

//***************  UPLOAD FILES ***********************************
	$source_file	  =	"Local-file.txt";
	$destination_file =	"Local.txt"; // after uploading file, rename as this name
	// upload the file
	$upload = ftp_put($ftp_connection,$destination_file,$source_file,FTP_ASCII );
	// check upload status
	if(!$upload){
		echo "File $source_file upload has failed!
" ; }else{ echo "Uploaded $source_file to $ftp_server as $destination_file
" ; }

3-Download files from server via FTP

//***************  GET FILES *************************************
	$fileName   = "Server-file.txt";
	$serverFile = fopen($fileName,"w");
	$localFile  = ftp_fget($ftp_connection, $serverFile, $fileName, FTP_BINARY);
	if (!$localFile)
	{
	   echo "Could not obtain $fileName file.
"; } else { echo "File downloaded successfully!
"; }

4-Get directory and files list

// *******************  GET DIRECTORY LIST ************************
    $fileList = ftp_nlist($connection_id, "."); // dot(.) means current directory
    foreach($fileList as $file){
     	echo $file, "
"; }

5-Create a directory

//******************  CREATE A DIRECTORY  *************************
	$dir = 'TestFolder';
	if (ftp_mkdir($connection_id, $dir)) {
	 	echo "Successfully created $dir
"; } else { echo "There was a problem while creating $dir
"; }

6-Delete a directory

// ******************* REMOVE FOLDER ******************************
	$dir = 'TestFolder';
	if (ftp_rmdir($connection_id, $dir)) {
		echo "Successfully deleted $dir
"; } else { echo "There was a problem while deleting $dir
"; }

7-Rename a directory or file

//********************  RENAME A DIRECTORY ***********************
	$old_file_name = 'location.php';
	$new_file_name = 'new_location.php';
	if (ftp_rename($ftp_connection, $old_file_name, $new_file_name)) {
	 	echo "Successfully renamed $old_file_name to $new_file_name
"; } else { echo "There was a problem while renaming $old_file_name to $new_file_name
"; }

8-Close the FTP connection

//***************  CLOSE THE CONNECTION ***************************
	ftp_close($ftp_connection);

Download source code (3 KB)