Verified:

Rip It Up Game profile

Member
768

Apr 21st 2013, 9:45:36

I have a html form atm that is able to write and append data into a csv file that already exists using PHP, but I want to add code in so that it checks to see if the csv file already exists, and if it doesn't, to create it and make it writeable, and insert the relevant column headings.


this is the code atm:

<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";

if(empty($_POST['formMovie']))
{
$errorMessage .= "<li>You forgot to enter a movie!</li>";
}
if(empty($_POST['formName']))
{
$errorMessage .= "<li>You forgot to enter a name!</li>";
}

$varMovie = $_POST['formMovie'];
$varName = $_POST['formName'];

if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varName . ", " . $varMovie . "\n");
fclose($fs);

header("Location: thank-you.html");
exit;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<title>My Form</title>
</head>

<body>
<?php
if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
?>
<form action="myform1.php" method="post">
<p>
What is your favorite movie?<br>
<input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>" />
</p>
<p>
What is your name?<br>
<input type="text" name="formName" maxlength="50" value="<?=$varName;?>" />
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>
</body>
</html>
Pain is weakness leaving the body.
MSN:

Dibs Ludicrous Game profile

Member
6702

Apr 21st 2013, 10:42:04

is there an fdir function?
There are no messages in your Inbox.
Elvis has left the building.

chuck

New Member
2

Apr 21st 2013, 16:40:35

First off, you should make sure you are sanitizing all user input. Trusting the contents of $_POST can lead to all sorts of security vulnerabilities.

It sounds like what you are looking for is the PHP file_exists function.

The manual entry:

http://php.net/.../function.file-exists.php

And an example usage:

$exists = file_exists('somefile.csv');
if(!$exists) {
// create the file if it was not found
}

// your file processing continues here.

Now, that said, if you just use the fopen function on a filename that is not found, PHP will attempt to create the file. specifically:

$my_csv = fopen('somefile.csv','a');

will open somefile.csv with the pointer at the end of the file (so that if you write to the file you will not overwrite its current contents) if somefile.csv exists and, if somefile.csv is not found by PHP, will attempt to create somefile.csv automatically.

Rip It Up Game profile

Member
768

Apr 21st 2013, 23:13:07

hmmm thanks chuck! :-) hopefully ill be able to work it out from here. thanks for pointing me in the right direction.
Pain is weakness leaving the body.
MSN:

Mr Emerald

Member
896

Apr 22nd 2013, 0:11:09

Also if you dont know, this will help with the sanitizing:

http://php.net/...lter.filters.sanitize.php


I dont know if it is a good list, but I haven't really used comma separated values very much... like once I can think of before I started sanitizing mysql inserts. lol
We are not the same, I am martian!
you are all retarded in the eyes of fluff
o o
( ._.) -----)-->
(_(' )(' )

RUN IT IS A KILLER TEDDY BEAR!!!

mrford Game profile

Member
21,378

Apr 22nd 2013, 5:23:13

NERDSSSSSSSSSSS!!!!!!!
Swagger of a Chupacabra

[21:37:01] <&KILLERfluffY> when I was doing FA stuff for sof the person who gave me the longest angry rant was Mr Ford