Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files from ftp server in PHP

Tags:

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 ?

like image 899
Sitansu Avatar asked Oct 17 '11 11:10

Sitansu


2 Answers

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);

?>
like image 117
user580950 Avatar answered Oct 10 '22 10:10

user580950


true == (
$data = @
    file_get_contents('ftp://username:password@server_name/folder_name/xyz#123.csv')
    )
        ?
    file_put_contents('xyz#123.csv', $data)
        :
    exit;
like image 20
admin Avatar answered Oct 10 '22 08:10

admin