Php script to download file from ftp server

I am using the following format in php code to download files from ftp server.

file_put_contents(
            $filePath.$fileName, file_get_contents(
                ftp://username:password@server_name/folder_name/xyz#123.csv.zip
            )
);    

I am getting an 550 error as the file name contains '#'. How to avoid the error. Also what is the best PHP class to do different operations on FTP ?

asked Oct 17, 2011 at 11:36

Php script to download file from ftp server

4

Use this


answered Feb 12, 2012 at 10:30

user580950user580950

3,41512 gold badges46 silver badges89 bronze badges

4

true == (
$data = @
    file_get_contents('ftp://username:password@server_name/folder_name/xyz#123.csv')
    )
        ?
    file_put_contents('xyz#123.csv', $data)
        :
    exit;

answered Oct 3, 2013 at 16:21

adminadmin

1672 silver badges5 bronze badges

Try:

$output = exec("wget -N ftp:///path/to directory/file 2>&1 |grep 'filename w/o extension'|grep -v ftp|grep -v =");
print $output 

answered Aug 2, 2013 at 15:20

1

Welcome to a quick tutorial on how to upload and download files through FTP using PHP. Need to manage some files on a server through an automated script?

To upload and download files using PHP FTP, we just need to enable the FTP extension in php.ini and use it accordingly:

  • $ftp = ftp_connect("HOST");
  • ftp_login($ftp, "USER", "PASSWORD");
  • Download files: ftp_get($ftp, "DESTINATION", "SOURCE", FTP_BINARY);
  • Upload files: ftp_put($ftp, "DESTINATION", "SOURCE", FTP_BINARY);
  • ftp_close($ftp);

That should cover the basics, but let us walk through a few actual examples in this guide – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

QUICK SLIDES

Php script to download file from ftp server

TABLE OF CONTENTS

DOWNLOAD & NOTES

Php script to download file from ftp server

Firstly, here is the download link to the example code as promised.

QUICK NOTES

  • Please make sure that the FTP and cURL extensions are enabled in php.ini accordingly – extension=ftp and extension=curl.
  • Captain Obvious. Remember to change the FTP settings to your own in the example scripts below.
  • This is only tested on the Filezilla FTP server and PHP 8.

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

In this first section, we will walk through how to use the “legit” PHP FTP extension to upload and download files.

DOWNLOAD FILES WITH PHP FTP 

1a-ftp-download.php

This should be very straightforward and easy to understand.

  • We use the ftp_connect() function to connect to an FTP server.
  • Next, log in to the FTP server using the ftp_login() function.
  • Then download the required files using ftp_get().
  • Finally, remember to close the FTP connection using ftp_close().

UPLOAD FILES WITH PHP FTP 

1b-ftp-upload.php

That’s right, this is pretty much the same. Except that to upload files, we use ftp_put() instead.

OTHER PHP FTP COMMANDS

1c-ftp-commands.php

Yes, it is also possible to get the current folder, change the folder, or get the list of files on the FTP server.

UPLOAD & DOWNLOAD USING PHP CURL

If you somehow cannot get PHP FTP to work properly – This is an alternative way of using PHP cURL to work with FTP.

DOWNLOAD FILES WITH CURL FTP

2a-curl-download.php

 $ftphost . $source,
  CURLOPT_USERPWD => "$ftpuser:$ftppass",
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_FILE => $file
]);

// (D) EXECUTE CURL (DOWNLOAD FILE)
curl_exec($curl);
 
// (E) CLOSE CONNECTION + FILE
curl_close($curl);
fclose($file);

Downloading a file through FTP cURL is a little more complicated, but still straightforward nonetheless:

  • We initialize cURL with curl_init().
  • Open and create an empty file on the server with fopen().
  • Set the cURL options with curl_setopt_array:
    • CURLOPT_URL Address of the FTP server
    • CURLOPT_USERPWD User and password.
    • CURLOPT_RETURNTRANSFER Set to return transfer, because we are downloading stuff.
    • CURLOPT_FILE Pointing this to the empty file we opened with fopen().
  • Execute cURL with curl_exec(), watch it do the magic.
  • Finally, remember to close the connection with curl_close(), and also the file with fclose().

UPLOAD FILES WITH CURL FTP

2b-curl-upload.php

 $ftphost . $destination,
  CURLOPT_USERPWD => "$ftpuser:$ftppass",
  CURLOPT_UPLOAD => 1,
  CURLOPT_INFILE => $file,
  CURLOPT_INFILESIZE => filesize($source)
]);
 
// (D) EXECUTE CURL (UPLOAD FILE)
curl_exec($curl);
 
// (E) CLOSE CONNECTION + FILE
curl_close($curl);
fclose($file);

Uploading is pretty much the opposite of downloading. Instead of creating an empty file on the server, we read from an existing file instead.

PHP FTP – A QUICK SUMMARY

Function Description
ftp_connect(URL) Connects to target FTP host.
ftp_login(STREAM, USER, PASSWORD) Login to the FTP server.
ftp_get(STREAM, DESTINATION, SOURCE, MODE) Downloads selected file.
ftp_put(STREAM, DESTINATION, SOURCE, MODE) Uploads selected file.
ftp_close(STREAM) Closes FTP connection.

PHP CURL FTP – A QUICK SUMMARY

Function Description
curl_init() Initialize cURL connection.
fopen(FILE, MODE) Opens a file for reading or writing.
curl_setopt_array(STREAM, ARRAY) Sets cURL options.
  • CURLOPT_URL: Host URL
  • CURLOPT_USERPWD: The user and password (user:password).
  • CURLOPT_RETURNTRANSFER: Returns transfer from the server? True or false.
  • CURLOPT_FILE: Target file to save to (download).
  • CURLOPT_UPLOAD: Has file upload? True or false.
  • CURLOPT_INFILE: Target file to upload.
  • CURLOPT_INFILESIZE: Size of file to upload.
curl_exec(STREAM) Executes cURL.
curl_close(STREAM) Closes cURL connection.
fclose(FILE) Commits and writes file properly.
  • PHP FTP – The official PHP manual.
  • PHP cURL – The official PHP manual.
  • Download Entire Folder With PHP FTP – Code Boxx

INFOGRAPHIC CHEAT SHEET

Php script to download file from ftp server
Upload Download Files Using FTP PHP (click to enlarge)

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

How to download file from ftp server in PHP?

PHP ftp_get( ) Function. The ftp_get() function is used to download a file from the FTP server. The ftp_get() function retrieves a remote file from the FTP server, and saves it into a local file. This function was introduced in PHP 4.

How to connect ftp server using PHP?

PHP ftp_connect() Function $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server"); $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass); // then do something... ftp_close($ftp_conn);

How do I get files from an FTP?

How to Copy Files From a Remote System ( ftp ).
Change to a directory on the local system where you want the files from the remote system to be copied. ... .
Establish an ftp connection. ... .
Change to the source directory. ... .
Ensure that you have read permission for the source files. ... .
Set the transfer type to binary..

How do I download a file from FTP in Python?

I suggest the use of with here which takes care of closing the file handle when done: with open(filename, "wb") as file: ftp.retrbinary("RETR " + filename, file.write) – Lekensteyn. ... .
FD leaks are no joke! ... .
use retrlines if the file is a text file..