FTP Upload with PHP for Beginners

Posted on Jun 27, 2013
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="get_upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">Current 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 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"></td>
</tr>
</table>
</form>

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 = $_POST['server'];
$ftp_user_name = $_POST['user'];
$ftp_user_pass = $_POST['password'];
$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 "Error: FTP upload has failed!";
} else {
echo "Good: Uploaded $name to $ftp_server";
}

For close the ftp connection we 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);

Leave a comment:

Thank you for your comment. After a while, our moderators will add it.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

© Twiwoo 2023 Cookie Policy