Sitansu
Sitansu

Reputation: 891

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 ?

Upvotes: 6

Views: 46447

Answers (4)

Softec
Softec

Reputation: 125

Download All files with original same name from FTP server and save to local

<?php
//Download All files with original same name from FTP server and save to local server using PHP 8.1
error_reporting(E_ALL);
 
$localFilePath = "/home/public_html/media/import/";
 
$ftp_server = "xxx"; 
$ftp_user_name = "xx"; 
$ftp_user_pass = "xxx";

$start_time = microtime(true);
 

function download($ftp,$directory,$localFilePath) {
    if (ftp_chdir($ftp, $directory)) {
        echo "Current directory is now: " . ftp_pwd($ftp) . "\n";
    } else { 
        echo "Couldn't change directory\n";
    }
    
    $fileContents = ftp_nlist($ftp, ftp_pwd($ftp));
    if(!$fileContents) {
        die(' no directory found');
    }
    $toReplace='/'.$directory.'/';
    foreach ($fileContents as $key => $fileName) {
         
        //echo $fileName; echo PHP_EOL;
        $onlyFileName = str_replace($toReplace,'',$fileName);
        echo $onlyFileName; echo PHP_EOL;
        
        $localFile = $localFilePath.$onlyFileName;
        
        //Target only file names which names are having only `-full` in it.
        if (strpos($onlyFileName, '-full') !== false) {
            if(ftp_get($ftp, $localFile, $fileName, FTP_BINARY)){
                echo "Successfully written to $localFile"; echo PHP_EOL;
            } else {
                echo "There was an issue"; echo PHP_EOL;
            }
        }
    }
}

try{
    $ftp = ftp_connect($ftp_server) or die('Could not connect to FTP server.');
    $login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);
    if (!$login_result) { 
        die('Could not log in to FTP account.');
    }
    ftp_pasv($ftp, true);
    
    //pass directory name taken from FTP server, in this case it's 'images'
    download($ftp,'images',$localFilePath);
    
    echo PHP_EOL;
    ftp_close($ftp);
    $execution_time = ();
    echo " Execution time of script = ".(microtime(true) - $start_time)." sec";
    echo PHP_EOL;
} catch(\Exception $e){
    print_r($e->getMessage());
}
 

Upvotes: 0

admin
admin

Reputation: 177

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

Upvotes: 2

run_time_rookie
run_time_rookie

Reputation: 69

Try:

$output = exec("wget -N ftp://[email protected]/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>

Upvotes: -2

TSCAmerica.com
TSCAmerica.com

Reputation: 5377

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

?>

Upvotes: 11

Related Questions