All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Download File From Server To Client

Last Updated : Mar 11, 2024

PHP Download File From Server To Client

In this article we will show you the solution of PHP download file from server to client, we can download files from server to client in many different methods.

In this tutorial, we will connect to the FTP server to transfer files from server to client.

To do this at first we have to access the FTP server first.

Step By Step Guide On PHP Download File From Server To Client :-

PHP code for config.php :

//FTP host setting
$ftp_host = "" ;
$ftp_user = "" ;
$ftp_pass = "abc123@" ;
  1. At first <?php tag to write the php.
  2. Adding information of host, user and password to the variables $ftp_host, $ftp_user and $ftp_pass
  3. Close the php code with ?> tag

PHP code for copy_file_To_remote_server.php :

require_once 'config.php' ;
require_once 'functions.php' ;
//ftp connection
$ftp_connection = ftp_connect($ftp_host) or die ("couldn't connect to $ftp_host") ;
ftp_login($ftp_connection, $ftp_user, $ftp_pass) or die ("couldn't login to the ftp server") ;
ftp_pasv($ftp_connection, true) ;
$local_dir = "C:\xampp\htdocs\talkerscode\destination" ;
$remote_Server_dir = "/talkerscode/image/image1" ;
$files = clean_scandir($local_dir) ;
pre_r(upload_files($ftp_connection, $local_dir,$remote_Server_dir)) ;
  1. At first <?php tag to write the php.
  2. Require_once to access the ‘config.php’ and ‘functions.php’
  3. Now for the FTP connection create a variable $ftp_connection.
  4. ftp_connect() with the FTP host declared in the config.php. if the connection failed using die() and display an error message
  5. ftp__login () function to login with the FTP server. If the connection failed use die() to display an error message.
  6. Using the ftp_pasv() function.
  7. $local_dir to add the file path
  8. $remote_Server_dir to add the remote folder address where the download is stored
  9. Then Clean_scandir() used with the $files function.
  10. Pre_r() and call the upload_files() function to display it.
  11. Close the php code with ?> tag

PHP code for function.php:

function upload_files($ftp_connection,$local_dir,$server_dir) {
    $files = clean_scandir($local_dir) ;
for ($i=0 ;$i<count($files);$i++) {
    $files_on_server = clean_ftp_nlist($ftp_connection, $remote_Server_dir) ;
    if(!in_array("$files[$i]", $files_on_server)){
            //upload images/files to remote server directory
            if(ftp_put($ftp_connection, "$remote_server_dir/$files[$i]", "$local_dir/$files[$i]", FTP_BINARY)) {
                echo "Successfully uploaded $files[$i]".BR;
            else {
                echo "problem occured while uploading $files[$i]".BR ;
    else {
        echo "$remote_server_dir/$$files[$i] exists !".BR ;
$files_on_server = clean_ftp_nlist($ftp_connection,$server_dir) ;
ftp_close($ftp_connection) ;
return $files_on_server ;
  1. At first <?php tag to write the php.
  2. Now create the function for upload_files(). Then used clean_scandir() within it
  3. Then create e for loop and again used clean_scandir() within it with $files_on_Server
  4. To upload files now create an if statement within it with the files array.
  5. Again create an if statement with ftp_put() and display that the file is successfully uploaded
  6. And also an else statement to display error message using echo
  7. Closing the FTP connection and return the $files_on_server.
  8. Close the php code with ?> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to download files from server to client using PHP.

I hope this article on PHP download file from server to client helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪