Upload any file to FTP Server using PHP code
In this article 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.
How to upload a file to ftp in php
In this coming part of code we will see step by step to how to upload any file to ftp server using php script.
Create a simple html form as sample code shown below.
In this example, we have created a simple HTML form called "uploadfile.html" by adding above code.
Now the next step is to create a new PHP file and name it as same (saveupload.php) what we have provided in the action attribute of form tag.
Create a new blank PHP file i.e. "saveupload.php" to collect the http posted values from the HTML form.
Create a mysql connection to the database on the server. We are connecting to mysql database because to store the file name in the database. So inorder to store any file path in the database first we have to make mysql connection to the database.
MYSQL Connection using PHP code
On top of "saveupload.php" file, Let's write the mysql connection code in php as shown below.
Once you have added the above code. Kindly do the re-check wheather mysql database is connecting successfully or not.
Settings for FTP connection
In the same file just below of your mysql connection, do some settings for the ftp connections and saved uploaded files directory.
"$domainname" php variable will be used to save file path in the database.
"$domainhost", "$domainuser" and $domainpass" these variables will used to connect to FTP server (FTP Web Server).
"$homelink" php variable will be used while deleting any file from the FTP Server.
"$publiclink" php variable will be used for both to upload or delete file from FTP Server
"$savedfiledirectory" php variable an important variable which specifies where your uploaded files will be saved on FTP server.
Kindly Note : Please use carefully "/" forward slash in the same order as shown in the above settings. Otherwise there might be chance of getting an error while uploading files.
Create FTP Connection Function
Now lets write a new function for ftp connection. This function basically make ftp connection and makes ftp-login by taking hots-name, user-name and pass-word and returning back the connection result. If the ftp-login is successful then it will return connect ID or else return 0.
Create Function to Close/Quit FTP Connection
In the above code we have created a function "ftp_connection()" which connects to FTP Web Server means ftp connection is open so its a security threat we need to close the FTP connection immediately once any file is uploaded to the server. So let's make a new function which will close or quit ftp connection once file uploaded to the server.
So we have added a new function called "ftp_connection_quit()" which takes input as connection id and passes that connection id tp two inbuilt PHP function "ftp_quit()" and "ftp_close()" to terminate ftp connection.
Create Function To Upload Files on FTP
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.
0644 means owner of that file / folder can "Read" and "Write"
0644 means group of that file / folder can "Read" only.
0644 means public of that file / folder can "Read" only.
0755 means owner of that file / folder can "Read", "Write" and "Execute".
0755 means group of that file / folder can "Read" and "Execute".
0755 means public of that file / folder can "Read" and "Execute".
0777 means owner of that file / folder can "Read", "Write" and "Execute".
0777 means group of that file / folder can "Read", "Write" and "Execute".
0777 means public of that file / folder can "Read", "Write" and "Execute".
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.
In the next line of code we have used php function "ftp_pasv()".
"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.
Get the Http post values and pass to "PutFilesonFTPServer()" function.
As you see in our final part of php code that we have collected http posted values i.e. every thing about the uploaded file information (file name, file type, file tmp name and file extension.
"$new_file_name" is set to todays date and current time.
"$imagespath" is set to image path for example : http://www.onlinebuff.com/myfiles/21-03-2014-1235465.jpg
Final Part of Code
We have set everything all variables is been set with proper values now its time to pass everything to our main function "PutFilesonFTPServer()" as input values.
$domainhost = "ftp.your-domain-name.com";
$domainuser = "ftp-user-name";
$domainpass = "ftp-pass-word"
$savedfiledirectory = "myfiles/";
$new_file_name = "21-03-2014-1235465.jpg";
$ftmp = $_FILES['uploadedfile']['tmp_name'];
Kindly set above variable values with your original values.
Store File Path in Mysql Database
This step is optional. If you want to store file path in the database then only use this step.
when "PutFilesonFTPServer()" function uploads the file successfully to remote server then we have made MYSQL data insert with imagepath and uploadedtime.
How to delete a file on ftp server in php
In the coming part of code we will see how to delete existing file on ftp web server using php code.
Above the function "DeleteFilesFromFTPServer()" which is used to delete files from the FTP Server. The function uses input values as domain host, domain-user-name, domain-pass-word, existing folder and file-name.
The function is similar like "PutFilesonFTPServer()" function. The only changes we have to make to remove "ftp_put()" and added "unlink()" function. Since "unlink()" function uses the full directory root path we have used extra global variable i.e. "$homelink" in the unlink function.
unlink function helps to delete any file or image from the ftp server folder.
Kindly note : To make unlink function work, folder permission is must set it to 0777 means public permission.
So friends this is how you can upload or delete any from FTP web server. If like this post then hit like button and share it with your friends on google+, facebook, twitter. If you any doubt or comments or feedback step by step procedure on uploading files to ftp server kindly let me know through your valuable comments.
By Subodh on 2014-11-22Thank you very much, This blog is very helpful for me.
By Crystal on 2014-05-07Hi Gurunatha, Thanks for the tutorial. How should I go about inserting an upload progress bar in to this? I believe it's not possible with ftp_put
Add a Comment
Thank you, for commenting !!
Your comment is under moderation...
Keep reading other blogs