PHP ftp upload tutorial

To upload a file to ftp you need a html form where you can insert the ftp details, like the ones described above:

server – is the server on which he wanna upload the file
username – is the user for which he makes the connection to ftp server
password – is the user password for ftp username
path to server – is the path on the ftp server which he wanna upload his file.
user file – is the file of the you wanna upload to ftp server

Hint: If you want to offer to your website members a form where he will upload his file, you should take the ftp server details and path, from a config file. Here we made a complete example how to upload through ftp putting all the details.

We will have a POST form and an enctype of multipart/form-data because we have a file on our form.
The user form that the user will insert info is like that:

<form action="upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">
Server:
</td>
<td>
<input size="50" type="text" name="server" value="">
</td>
</tr>
<tr>
<td align="right">
Username:
</td>
<td>
<input size="50" type="text" name="user"  value="">
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input size="50" type="text" name="password" value="" >
</td>
</tr>
<tr>
<td align="right">
Path on the server:
</td>
<td>
<input size="50" type="text" name="pathserver" >
</td>
</tr>
<tr>
<td align="right">
Select your file to upload:
</td>
<td>
<input name="userfile" type="file" size="50">
</td>
</tr>
</table>
<table align="center">
<tr>
<td align="center">
<input type="submit" name="submit" value="Upload image" />
</td>
</tr>   </table>
</form>

Now, when the user submits the form, we should get all the details that he inputed and then upload the file to the ftp server.

// the file name that should be uploaded
$filep=$_FILES['userfile']['tmp_name']; 
// ftp server
$ftp_server=$_POST['server'];
//ftp user name
$ftp_user_name=$_POST['user'];
//ftp username password
$ftp_user_pass=$_POST['password'];
//path to the folder on which you wanna upload the file
$paths=$_POST['pathserver'];
//the name of the file on the server after you upload the file
$name=$_FILES['userfile']['name'];

To upload the file , first we should establish a connection to the ftp server using ftp_connect and specifing as a parameter the ftp servver. This function return a connection id.

$con_id=ftp_connect($ftp_server);

After connecting to server we should login using the function ftp_login passing three parameters: connection id, ftp user, ftp user password and checking if the login was sucessfull

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has failed!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name....";
       exit;
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name".".....";
   }

After loging in we can upload the file to the server and after that check if the file was uploaded correctly:

// upload the file
$upload = ftp_put($conn_id, 'public_html/'.$paths.'/'.$name, $filep, FTP_BINARY);   // check upload status
if (!$upload) {
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $name to $ftp_server ";
   }

In the end we should close the ftp connection using ftp_close passing the connection id:

ftp_close($conn_id);

For uploading big files you should set the time limit of the server in order not to finish the script while it’s uploading:

set_time_limit(300);

Here is the complete code:

<?
if(!isset($_POST["submit"])){?>   <form action="upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">
Server:
</td>
<td>
<input size="50" type="text" name="server" value="">
</td>
</tr>
<tr>
<td align="right">
Username:
</td>
<td>
<input size="50" type="text" name="user"  value="">
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input size="50" type="text" name="password" value="" >
</td>
</tr>
<tr>
<td align="right">
Path on the server:
</td>
<td>
<input size="50" type="text" name="pathserver" >
</td>
</tr>
<tr>
<td align="right">
Select your file to upload:
</td>
<td>
<input name="userfile" type="file" size="50">
</td>
</tr>
</table>
<table align="center">
<tr>
<td align="center">
<input type="submit" name="submit" value="Upload image" />
</td>
</tr>   </table>
</form>
<?}
else 
{   set_time_limit(300);//for setting   $paths=$_POST['pathserver'];   $filep=$_FILES['userfile']['tmp_name'];   $ftp_server=$_POST['server'];   $ftp_user_name=$_POST['user'];   $ftp_user_pass=$_POST['password'];   $name=$_FILES['userfile']['name'];       // set up a connection to ftp server
$conn_id = ftp_connect($ftp_server);   // login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);   // check connection and login result
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has encountered an error!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name....";
       exit;
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name".".....";
   }   // upload the file to the path specified
$upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);   // check the upload status
if (!$upload) {
       echo "FTP upload has encountered an error!";
   } else {
       echo "Uploaded file with name $name to $ftp_server ";
   }   // close the FTP connection
ftp_close($conn_id);   }
?>
Download source code

http://codestips.com/php-ftp-upload-tutorial/

Leave a comment