I try to generate a CSV file and send it to FTP account.
My problem is to popolate the CSV file.
This code work well, but I can pass only string in $fp. When I try to use an Array the file arrive to the ftp account empty.
I can't understand why.
Also, I now try to pass an Array just for test, but the best for me is will be able to use an Object, How I can generate this file?
function ftp_file_put_contents($remote_file, $file_string) {
// FTP login details
$ftp_server="ftp.xxx.it";
$ftp_user_name="xxx@xx.it";
$ftp_user_pass="xxxx";
// Create temporary file
$local_file = fopen('php://temp', 'r+');
fwrite($local_file, $file_string);
rewind($local_file);
// FTP connection
$ftp_conn=ftp_connect($ftp_server);
// FTP login
@$login_result=ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
// FTP upload
if($login_result)
$upload_result=ftp_fput($ftp_conn, $remote_file, $local_file, FTP_ASCII);
// Error handling
if(!$login_result) {
echo('<p>FTP error: The file could not be written to the FTP server perche $login_result.</p>');
} elseif (!$upload_result) {
echo('<p>FTP error: The file could not be written to the FTP server perche $upload_result.</p>');
} else {
echo('<p>good</p>');
echo $file_string;
}
// Close FTP connection
ftp_close($ftp_conn);
// Close file handle
fclose($local_file);
}
$Fname = 'invoce.csv';
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen($Fname, 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
// Function call
ftp_file_put_contents($Fname, $fp);