i managed to put together this code for download a txt file from FTP (It’s like a csv file with ‘ ; ‘ delimiter and there is almost 900 products in this file).

It works great… but i’m wonder if there is a way to format the price column wich comes with 4 decimals (Example: 3,1628). I would like to format this prices to 2 decimals in this column.

Here is my code:

    function download_file(){  

// FTP credentials     
$ftp = array( 
   'server' => 'ftp.url.com', 
   'user' => 'user', 
   'pass' => 'password' 
);   

// Connect 
$ftp_connection = ftp_connect( $ftp['server'] ) or die( 'Could not connect to $ftp[$server]' ); 

if ( $login = ftp_login( $ftp_connection, $ftp['user'], $ftp['pass'] ) ) { 

// Set passive mode 
  ftp_pasv( $ftp_connection, true ); 

// Login passed
  echo "Ok!!!";

// Date to download latest file   
   $fechas = date('d'.'m'.'y');

// Download data to this file. 
   $local_file = dirname(__FILE__).'/../../../catalogo/datafeed.txt'; 

// Path and filename to open and read from.                                                     
   $remote_file = '/catalogo_40888_'.$fechas.'.txt'; 

   $file = ftp_get( $ftp_connection, $local_file, $remote_file, FTP_ASCII );

// close ftp  
ftp_close($ftp_connection); 
}
}

I don’t know if there is an easy way to do this. Any help would be appreciated, thank u guys.

Read more here: PHP number_format or round to 2 decimals while downloading TXT file


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: