File empty after fputcsv

monkeyUser

I created a file with fputcsv

    $handle = fopen('php://output', 'w+');

    // Add the header of the CSV file
    fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
    // Query data from database

    // Add the data queried from database
    foreach ($results as $result) {
        fputcsv(
            $handle, // The file pointer
            array(...), // The fields
            ';' // The delimiter
        );
    }

    file_put_contents('mycsv.csv',fgetcsv($handle), FILE_APPEND);

    fclose($handle);
 ....

I want to save the output on mycsv.csv but the file is empty

James

The php://output is a stream that works like echo or print. It means, you are writing in the standard output (maybe, your console or the browser).

If you want to write your content in a csv file, try to open this file or create it using PHP directly, instead of use the file_put_contents.

$handle = fopen("mycsv.csv","wb");
fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'), ';');
//...put your code
rewind($handle);//optional.it will set the file pointer to the begin of the file

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

File content is not available after fputcsv

Eclipse - File is empty after crash

Reversed file is empty after piping

fputcsv is putting duplicate values in my file

Adding BOM to CSV file using fputcsv

fputcsv adds unwanted Validation comment at top of file

avoid blank line fputcsv file download code

Python File Remains Empty After Writing to it Issue

Add an empty line after a specific line in the file

Redis is empty after startup, altough there is an .rdb file

Getting empty result after parsing xsd file

Empty image file after uploading to a server

File is empty after creating it using Uri - Retrofit

SSIS - XML file empty after rename task

$_POST and $_FILES empty after AJAX file upload

empty List after converting csv file to dictionary

ARM Linux file empty after reboot

Empty CSV file after recode via BATCH

PDF file empty after outputting with Jersey

Iterating the bytes of a File is empty after iterating the lines of the same file

Use fputcsv create a .csv file and also update the page with table data

PHP | fputcsv : generated file only populated with field header, but no records

One of the dist file is always empty after running webpack

Why is my XML file empty after generating with XML::LibXML

Recover Missing File after `read-tree --empty` and `reset --hard`

selecting random line from text file after deleting empty lines

Saving a figure after invoking pyplot.show() results in an empty file

jQuery DataTables - File becomes empty after dynamic change of filename

End_of_stream property after opening empty file