I'm trying to save some variables in .csv file which is online, but it doesn't work. I tried to create the file by myself and run the code and to let the code creates the file when it runs. But, in both cases it doesn't work
<html>
<body>
<?php
$name = $_POST['name'];
$gender = $_POST['gender'];
$nationality = $_POST['nationality'];
if($nationality == "Other")
$nationality = $_POST['otherNationality'];
$job = $_POST['job'];
$country = $_POST['country'];
$years = $_POST['years'];
$fh = fopen("results.csv", "a");
$personalInfo = $name. ", " . $gender . ", " . $nationality . ", " . $job . ", " . $country . ", " . $years;
if($fh){
fwrite($fh, $personalInfo);
fclose($fh);
}
?>
There does not seem to be anything wrong when it comes to the csv writing - as long a the form fields are initialized.
What you want to make sure is you have permissions to write to that file
_ if this is windows, create the file and give everybody permission to write
_ if this is linux:
touch results.csv;
chmod 777 results.csv
Alternatively, try executing it from the commandline php csvscript.php
As I said it was a permission issue and since you have no access to the CLI you can use the following
$name = $_POST['name'];
$gender = $_POST['gender'];
$nationality = $_POST['nationality'];
if($nationality == "Other")
$nationality = $_POST['otherNationality'];
$job = $_POST['job'];
$country = $_POST['country'];
$years = $_POST['years'];
$opr = chmod ( "results.csv" , 0755 );
if($opr)
{
$fh = fopen("results.csv", "a");
$personalInfo = $name. ", " . $gender . ", " . $nationality . ", " . $job . ", " . $country . ", " . $years;
if($fh){
fwrite($fh, $personalInfo);
fclose($fh);
}
}
else
{
echo "Not able to change the permission :( ";
}
I hope this help
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.