我是PHP新手,无法弄清楚这一点。
我有2个输入(姓名和电子邮件)。 我需要将其写入CSV文件并单击下载。 我发现我需要fputcsv()函数并形成数组数组......
但不知怎的,我不知道如何正确地形成这个数组......有人可以给我一个提示吗?
我已经知道了txt文件......但是根本无法将其转换为csv ...
public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . '-' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.txt', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }Im new to PHP and cant figure this out.
I have 2 inputs(Name and email). I need to write it to CSV file and download on click. I found out that i need fputcsv() function and to form array of arrays...
But somehow i cant see how to form that array correctly... Can someone give me a hint?
Im already wirting to txt file... but simply cant transform it to csv...
public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . '-' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.txt', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }最满意答案
您需要做的唯一事情是用“,”替换subName和subEmail之间的短划线,并将您的文件重命名为subs.csv。
public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . ',' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.csv', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }CSV不是特殊的文件格式。 它只是具有特定格式的纯文本。
The only thing you need to do is replace the dash between subName and subEmail with a "," and rename your file to subs.csv.
public function invokeAction(CakeRequest $request){ if ($this->request->is('post')) { if(isset($_POST['subName']) && isset($_POST['subEmail'])) { $data = $_POST['subName'] . ',' . $_POST['subEmail'] . "\n"; $ret = file_put_contents('../tmp/subs.csv', $data, FILE_APPEND | LOCK_EX); unset($_POST['subName']); unset($_POST['subEmail']); } }CSV is not a special file format. It's just plain text with a certain format.
更多推荐
发布评论