PHP code not creating file

I am looking for some help on the following code I am working on. This code consist of a form in PHP to write entries inside the fields and take the fields entries and generate a CSV file on my server.

The problem is the file is not being generated on my server. I am not really a guru on PHP but I gotten so far that my php form is collecting the entries from the fields and making the filename csv which is good.

My code:
[php]if(isset($_POST[‘submit’])){

//collect form data
$location = $_POST['location'];
$ID = $_POST['ID'];
    $section = $_POST['section'];

//check name is set
if($location ==''){
	$error[] = 'Name is required';
}

//if no errors carry on
if(!isset($error)){

	# Title of the CSV
	$Content = "location, ID, section\n";

	//set the data of the CSV
	$Content .= "$location, $ID, $section\n";

# set the file name and create CSV file
$my_file = ("$location$ID$section.cvs");
$handle = fopen("$my_file", "w") or die('Cannot open file:  '.$my_file);
header('Content-Type: application/csv'); 
header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
echo $Content;
exit();
}

}

//if their are errors display them
if(isset($error)){
foreach($error as $error){
echo “

$error

”;
}
}
?>
[/php]

After this my browser tells me:
Cannot open file: BOD10Buffer.cvs

So it cannot make open the csv and it wont create it on my server either.
I wanted to check if there might be anyone that could help me? Is there something I might be doing wrong? I know it’s not coded nicely and I apologize for this. Any help would be much appreciated.

Make sure you have write permissions set on the folder you are trying to write to. (Assuming code works)

Hi Randolph Willems,

Above codes seem didn’t write any content into the file ? Please check my code as below , it is working fine in my WAMP , you can try on your server ^^"

[php]
//set file name with date format
$date = date_create($row[0]);
$dateFormat=date_format($date, ‘dMY_ha’);

//combine the file name with .txt
$fileContents= $dateFormat.".txt";

//creating the new file on local
$updatedcountrytxt = fopen($fileContents, “a+”);

//define what data you want to write
$data=‘what data you want to write into your file’;

//writing the data into the file
file_put_contents($fileContents, $data, FILE_APPEND);

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service