如何使用PHP创建CSV文件(并上传)

如何使用PHP创建CSV文件(并上传)

问题描述:

例如,我有一个变量$ foo,其中包含了我想在CSV中显示的所有数据:

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";

我的目标是:


  1. 创建名为some.csv的CSV文件,其内容等于$ foo

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

上传some.csv

Upload "some.csv" to my server.

如何做到这一点?

更新:以下是适用于我的确切代码。

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


Number 1:

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

第2个:

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

请注意文件前面的@