Borja León Borja León - 9 months ago 64
PHP Question

Open CSV file and replace content if is empty

I am running somo Mysqli querys that generates me CSV output files. Some of them are just empty CSV files because nothing matches with the query parameters. I want a script that opens and replaces the content (only if the file is empty) of all CSV files in a folder.

It can be in PHP but also awk, sed or similars.


Answer Source

In bash:

file=foo; if [ -w "$file" -a ! -s "$file" ] ; then echo "./." > "$file" ; fi

If file foo exist (-w) and (-a) is a zero-length file (! -s) write ./. over its empty contents. You can test it by touch foo and then running that script.