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

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

TABLE OF CONTENTS

DOWNLOAD & NOTES

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

Chủ Đề