Hi, in today's post we will see how to upload files to ftp server using php script. File management via FTP is an essential skill for a web developer. A good FTP client would handle FTP communication, upload and download files from FTP server efficiently. But at times you may want to do it programmatically. With PHP, the native FTP functions allow you to easily handle all kinds of file operations with ease. Uploading files using PHP FTP functions is almost similar to doing it with an FTP client. Come on, I'll show you how to transfer files to the server through ftp protocol in php.
Connecting FTP Server with PHP:
When you work with FTP protocol, you must establish a client-server connection. To do this, you must first connect to the FTP server and then log on using the credentials.
The following are the two PHP functions you need for this step.
- ftp_connect(ftp_host)
- ftp_login(ftp_connection, ftp_username, ftp_password)
Note that you need permission to log in and upload files to ftp server. So make sure you have it first.
Once you are logged in to the ftp server, you can upload files using,
ftp_put(ftp_connection, remote_file, source_file, transfer_mode)
This will transfer the source file to the remote directory.
Once you finish uploading, close the FTP stream this way,
ftp_close(ftp_connection)
How to Upload Files to FTP Server with PHP?
Now we know all the required PHP functions for FTP file transfer. Here I have created a PHP demo where you can perform file upload through the FTP protocol.
STEP-1) Create File Upload Form:
Build a simple file upload form with a file input and a submit button.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Upload Files to FTP Server in PHP</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" >
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<br/>
<div class="container">
<div class="col-xs-8 col-xs-offset-2 well" style="background:none;">
<form action="ftp_upload.php" method="post" enctype="multipart/form-data">
<legend>Please Choose File to Upload</legend>
<div class="form-group">
<input type="file" name="srcfile" />
</div>
<div class="form-group">
<input type="submit" name="submit" value="Upload File to FTP Server" class="btn btn-warning"/>
</div>
</form>
</div>
</div>
</body>
</html>
STEP-2) PHP FTP Uploader:
Once the form has been submitted, the FTP upload script will transfer the file to the FTP server using PHP's FTP functions we discussed earlier.
ftp_upload.php
<?php
//check if form is submitted
if (isset($_POST['submit']))
{
// ftp settings
$ftp_hostname = 'ftp.example.com'; // change this
$ftp_username = 'ftpusername'; // change this
$ftp_password = 'ftppassword'; // change this
$remote_dir = '/path/to/folder'; // change this
$src_file = $_FILES['srcfile']['name'];
//upload file
if ($src_file!='')
{
// remote file path
$dst_file = $remote_dir . $src_file;
// connect ftp
$ftpcon = ftp_connect($ftp_hostname) or die('Error connecting to ftp server...');
// ftp login
$ftplogin = ftp_login($ftpcon, $ftp_username, $ftp_password);
// ftp upload
if (ftp_put($ftpcon, $dst_file, $src_file, FTP_ASCII))
echo 'File uploaded successfully to FTP server!';
else
echo 'Error uploading file! Please try again later.';
// close ftp stream
ftp_close($ftpcon);
}
else
header('Location: index.php');
}
?>
In the above script, the ftp_connect()
and ftp_login()
functions are used to establish the connection to the provided ftp host and log in with the ftp credentials.
After successful logon, the ftp_put()
function transfers the local file to the remote directory on FTP server. The function returns true or false based on the upload process and the success or error message will be displayed accordingly.
Once the file transfer is completed, the ftp stream will be closed using the ftp_close()
function.
- How to Download File from Remote URL in PHP & CURL
- Upload, View and Download File using PHP and MySQL
That's it! I hope you now understand better uploading files to the ftp server using php. Just make sure you have FTP access and use the right host name and credentials of the FTP server. If you find this tutorial useful, please share it on social networks :)