0

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);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Marco Romano
  • 272
  • 1
  • 14

1 Answers1

1

This code is already creating a file to upload.

$fp = fopen($Fname, 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

Now you can directly upload that file ($Fname) using ftp_put:

$upload_result = ftp_put($ftp_conn, $remote_file, $Fname, FTP_ASCII);

All your code with php://temp is pointless, as it just (tries) to create another copy of the file - remove it.


Though I assume that you actually wanted to avoid creating the local file altogether. As you code is obviously based on Creating and uploading a file in PHP to an FTP server without saving locally.

Then you need to do this:

$local_file = fopen('php://temp', 'r+');

foreach ($list as $fields) {
    fputcsv($local_file, $fields);
}

rewind($local_file);    

// ...

$upload_result = ftp_fput($ftp_conn, $remote_file, $local_file, FTP_ASCII);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • yes I was using that code. In ths way I have to move all the Array $list inside the function. what I pass in $file_string ? I don't need use $fp exatly? – Marco Romano Dec 13 '18 at 14:20
  • 1
    Or pass `$local_file` to the function (but call call `fclose` only after `ftp_file_put_contents`) - Or just inline the `ftp_file_put_contents` function. – Martin Prikryl Dec 13 '18 at 14:21
  • 1
    Work :) wow thank you. Now I think I understood I was re-writing the temporary file, and also the external file could not pass because it was closed before the execution of the function – Marco Romano Dec 13 '18 at 14:29
  • sorry @MartinPrikryl, If I want put inside the $list an Objct, what I can do? to have the CSV file like 2 row: the first key in the second value – Marco Romano Dec 13 '18 at 14:35
  • We do not know anything about your "object". So I cannot tell you how to represent that as CSV. That's for separate question. – Martin Prikryl Dec 13 '18 at 14:40
  • yes sure. Thank you anyway. Now I try to pass a second parameter with like argoment an a stdClass Object. ans try to keep the $list point to this parameter – Marco Romano Dec 13 '18 at 14:47