简体   繁体   中英

Array will not write to .csv file using HTML and PHP

I am trying to get a feedback form to write the survey responses from the HTML form into a .csv file to my directory, using PHP. The directory already has permissions to be written to, from a website. However, the file still won't write to the .csv file. I have checked and the data is being stored in the array, but it won't write into the .csv file. Here is the PHP code:

<?php

$understand = $_POST["understand"];
$helpful = $_POST["helpful"];
$rank = $_POST["rank"];

$list = array($understand, $helpful, $rank);

echo $list;

echo '<pre>'; print_r($list); echo '</pre>';

var_dump($list);

$file = fopen("/survey-answers/mylist.csv","a");
if(!$file) echo("<pre>Can't open file </pre>");

foreach ($list as $line)  {
fputcsv($file,explode(',',$line));  }

fclose($file);

?>

Most probably you have an issue with this line:

$file = fopen("/survey-answers/mylist.csv","a");

This tells PHP to open aa directory in the path /survey-answers/ , which mostly doesn't exist. If you are trying to save your CSV into a sub-directory of the current directory then you need to change that to:

$file = fopen("./survey-answers/mylist.csv","a");

or if you want to save it into a sub-directory of parent directory then you need to change that to

$file = fopen("../survey-answers/mylist.csv","a");

The path is very important to be correct in order your code to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM