Home /Core PHP /Upload any file to FTP Server using PHP

Upload any file to FTP Server using PHP

we will see step by step to upload any file or to delete any file from ftp server using php ftp functions. PHP scripting language offers some of inbuilt FTP functions, so we will see how to use those functions to upload a file or to delete a file from ftp server.

Create Function To Upload Files on FTP

function PutFilesonFTPServer($host, $user, $pass, $folder, $newfilename,$existingfilename){

global $publiclink;

$uploaded = false;

$conn = ftp_connection($host, $user, $pass);

if($conn == 0){

exit(“Error while connecting FTP Server”);

}

@ftp_set_option($conn, FTP_TIMEOUT_SEC, 1000);

$path = $publiclink.$folder;

@ftp_site($conn,”CHMOD 0777 $path.”);

@ftp_pasv($conn, true);

if(!@ftp_put($conn, $path.$newfilename, $existingfilename, FTP_BINARY)){

$uploaded = false;

}else{

$uploaded = true;

}

@ftp_site($conn,”CHMOD 0755 $path.”);

ftp_connection_quit($conn);

return $uploaded;

}

As you see in our above code that we have created a new function called “PutFilesonFTPServer()” which takes input as host name, host-user-name, host-pass-word, folder-name, new-file-name and existing-file-name.

PutFilesonFTPServer()” is the main function of our code to upload any file to ftp web server. It combines or it uses both above functions “ftp_connection()” and “ftp_connection_quit()“.

At start we have used global variable “$publiclink” which consist of public html path and then a new variable “$uploaded” this set to false this variable will be used to check wheather the file is been uploaded or not.

In the next line of code we have used “ftp_connection()” function to make ftp connection to the server and passed the input values i.e. host-name, host-user-name and host-pass-word.

If the ftp connection is successfull then the “ftp_connection()” function will return connection ID else it will return 0.

In the next line of code we have used php function “ftp_set_option()” to set the network timeout for 1000 seconds for the specified FTP stream.

We have made a new variable “$path” which is a glue of two variables global “$publiclink” and “$folder“.

We have used php function “ftp_site()” function to change the file permissions. On the FTP there are three types of permissions owner permissions, group permissions and public permissions.

Why we have used ftp_site() function

ftp_site()” function is used because on ftp server files or folders is usually with 0644 mode or 0755 mode. So in order to upload any file on ftp server from website we need to change folder permission mode to 0777. 0777 mode folder where any anonymous can upload file to that folder with 0777 mode.

So we have used “ftp_site()” function to change the folder mode to 0777 mode.

@ftp_site($conn,”CHMOD 0777 $path.”);

In the next line of code we have used php function “ftp_pasv()“.

@ftp_pasv($conn, true);

ftp_pasv()” function takes the connection ID and ftp_pasv() can only be called after a successfull login or otherwise it will fail.

Passive mode is needed only when the client is behind the firewall. Passive mode is used only when FTP cannot able to establish the data channel due to network firewalls where client can able to open up server but server may not have the power to open up the data channel back through firewall protection.

“ftp_pasv()” function ensures that data channel from both end will be successful even when the client is behind the firewall.

So for safe side use “ftp_pasv()” and make FTP connection in passive mode.

So all done now finally we will use the “ftp_put()” an inbuilt php function which uploads local file to remote server.

“ftp_put()” function needs connection ID, file path with new file name and local file with file transfer mode FTP_ASCII or FTP_BINARY then if the “ftp_put()” function uploads the file successfully then we have set “$uploaded” variable to true else false.

Finally again changing the file permission mode to 0755. So this is all about insights of “PutFilesonFTPServer()” function.

Leave a Reply