:: SHARE THIS POST ::
Checked and Unchecked operators in C# Step By Step
In this article we will understand use of or working with Checked and Unchecked operators in C# Step By Step using example ....Click To Know More
Remember Top 3 important points of out and ref in C#
3 important points to remember while working with out and ref in C# ....Click To Know More
Install SQL Server 2014 Step by Step with example
In this article learn to install SQL Server 2014 step by step using our example image screen shots without any errors ....Click To Know More
Type Safe in C#.NET step by step using example
Here in this article let's understand Type Safe in CSharp.NET step by step with an example ....Click To Know More
App Domain in C#.NET Step by Step using example
In this article let's understand about app domain in csharp(C#).NET step by step using simple example. ....Click To Know More
Delay signing in C# with example
In this article athesham sir talks about delay signing and discuss Delay signing definition in more detail ....Click To Know More
Tuples in c# example step by step
In this Part 1 of the article we learn about Tuples which is not new to programming languages but it was new to C# when introduced in C# 4.0. We will see ....Click To Know More
C# Anonymous Types vs Tuples with an example
This article is the continuation of previous part 1 in which we had earlier seen how tuples reduces the code by making it more tidy and readable ....Click To Know More
Anonymous Methods in C# with an example Step by Step
This article written by "Ahtesham Shaikh" in this article understand Anonymous Methods in C# with an example Step by Step ....Click To Know More
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.
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.
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.
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.
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.
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".
"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.
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.
More PHP Articles To Read Next:
Step by step to upload an image and store in database using php
Views - 169758 |Category - PHP
How to get Date and Time using PHP
Views - 13032 |Category - PHP
What is PHP with a simple example using PHP
Views - 11381 |Category - PHP
All fields marked with * are mandatory
Posted by Subodh on 2014-11-22
Thank you very much, This blog is very helpful for me.
Posted by Crystal on 2014-05-07
Hi 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