Viewed   116 times

For example, I have a variable "$foo" that includes all the data which I want to show in the CSV:

$foo = "some value,another value,last value";

My goal is to:

  1. Create a CSV file named "some.csv" whose contents are equal to $foo

  2. Upload "some.csv" to my server.

How can this be done?

Update: Here's the exact code that worked for me.

$foo = "some value,another value,last value";
$file = 'some_data.csv';
file_put_contents($file, $foo);



Number 1:

file_put_contents("foobar.csv", $yourString);

Number 2:

$c = curl_init("http://"...);  
curl_setopt($c, CURLOPT_POSTFIELDS, array('somefile' => "@foobar.csv"));
$result = curl_exec($c);

note the @ before the filename

Monday, August 29, 2022

Well this is embarrassing... I found the solution I was looking for and it couldn't be simpler. I used the following code to get the desired result.

<input id="fileSelect" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/" />  

Valid Accept Types:

For CSV files (.csv), use:

<input type="file" accept=".csv" />

For Excel Files 97-2003 (.xls), use:

<input type="file" accept="application/" />

For Excel Files 2007+ (.xlsx), use:

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />

For Text Files (.txt) use:

<input type="file" accept="text/plain" />

For Image Files (.png/.jpg/etc), use:

<input type="file" accept="image/*" />

For HTML Files (.htm,.html), use:

<input type="file" accept="text/html" />

For Video Files (.avi, .mpg, .mpeg, .mp4), use:

<input type="file" accept="video/*" />

For Audio Files (.mp3, .wav, etc), use:

<input type="file" accept="audio/*" />

For PDF Files, use:

<input type="file" accept=".pdf" /> 



If you are trying to display Excel CSV files (.csv), do NOT use:

  • text/csv
  • application/csv
  • text/comma-separated-values (works in Opera only).

If you are trying to display a particular file type (for example, a WAV or PDF), then this will almost always work...

 <input type="file" accept=".FILETYPE" />
Friday, October 28, 2022

Well, you'll have to first create the zipfile, using the ZipArchive class.

Then, send :

  • The right headers, indicating to the browser it should download something as a zip -- see header() -- there is an example on that manual's page that should help
  • The content of the zip file, using readfile()

And, finally, delete the zip file from your server, using unlink().

Note : as a security precaution, it might be wise to have a PHP script running automatically (by crontab, typically), that would delete the old zip files in your temporary directory.

This just in case your normal PHP script is, sometimes, interrupted, and doesn't delete the temporary file.

Friday, November 11, 2022

If you need to select several cases, the best way is to make a multi-dimensional array in php. Otherwise, just throw a few counters into your while loops and done.

$row = 1;
$mycsvfile = array(); //define the main array.
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>n";
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />n";
        $mycsvfile[] = $data; //add the row to the main array.

echo $mycsvfile[3][1]; //prints the 4th row, second column.
Sunday, December 4, 2022

From a 2 second Google and the PHP docs:

$csv = array_map('str_getcsv', file('data.csv'));

Then you can loop through the Array and UPDATE your DB

Thursday, August 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :