简体   繁体   中英

Download files from ftp server in PHP

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 ?

Use this

<?php

// define some variables
$local_file = 'filename.jpg';
$server_file = 'filename.jpg';
$ftp_server="www.abc.com";
$ftp_user_name="username";
$ftp_user_pass="password";

$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
}
else {
    echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);

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

Try:

$output = exec("wget -N ftp://username@ftp.server.com/path/to directory/file 2>&1 |grep 'filename w/o extension'|grep -v ftp|grep -v =");
print $output <to check if file downloaded or not>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM