Trouble with PHP and saving to CSV file

I am a super novice. It usually takes me 6 to 8 hours of trial and error for me to figure out something that is probably simple to everyone on here. I’ve finally met my match. I was simply adding a PHP code to save form info to a CSV file, but I keep getting errors. The error I get is:

Warning: fopen(, ) [function.fopen]: failed to open stream: Invalid argument in www.mywebsite.com/webpage.php on line 93

Here is the code:
[php]
$filelocation= $_SERVER[‘httpdocs/formtest.csv’] .

$cvsData = $Name . “,” . $phone . “,” . $phone2 . “,” . $Email . “,” . $stories . “,” . $squarefootage . “,” . $screens . “,” . $shingles . “,” .$numbgutters . “,” . $screenquote . “,” . $treepruning . “,” . $skylights . “,” . $zipcode . “,” . $hear ."\n";

$fp = fopen("$filelocation",“a”);
if($fp){
fwrite($fp,$cvsData); // Write information to the file
fclose($fp); // Close the file
echo “File saved successfully”;
} else {
echo “Error saving file!”;
}[/php]

Please help and, if at all possible, dumb down the suggestion so I can understand.

try this instead:

[PHP]
$filelocation = $_SERVER[‘DOCUMENT_ROOT’].’/formtest.csv’;
[/PHP]

I don’t think you can use the location/filename itself inside the $_SERVER variable…
also, this is assuming your DOCUMENT_ROOT is httpdocs/

Thank you for helping. It’s different now, but still not working. I made the change, and

Warning: fopen(/formtest.csv) [function.fopen]: failed to open stream: Permission denied … on line 93

I’ve changed the permissions to 777. I’m out of ideas.

here is how the code looks now:

[php]$filelocation = $_SERVER[‘DOCUMENT_ROOT’].’/formtest.csv’;

$cvsData = $Name . “,” . $phone . “,” . $phone2 . “,” . $Email . “,” . $stories . “,” . $squarefootage . “,” . $screens . “,” . $shingles . “,” .$numbgutters . “,” . $screenquote . “,” . $treepruning . “,” . $skylights . “,” . $zipcode . “,” . $hear ."\n";

$fp = fopen("$filelocation",“a”);
if($fp){
fwrite($fp,$cvsData); // Write information to the file
fclose($fp); // Close the file
echo “File saved successfully”;
} else {
echo “Error saving file!”;
}[/php]

well it seems like it would work if the permissions were good… this i can’t really say… is it your own machine? a different machine? windows? linux? did you try setting the folder that contains the file in the 700’s also? if you can’t get access to the folder, the permissions on the file is pointless… if it’s a web host, maybe try contacting them for permission errors… i’m gonna assume its linux since the / works… windows uses \, not /… sorry i can’t be of much more help at this point… do let me know if you solve this issue… i’m curious what the problem is :slight_smile:

Well, from the “$filelocation= $_SERVER[‘httpdocs/formtest.csv’]” line in your original post, I would think you are using a local server on your own computer.

If this is true use $filelocation=“formtest.csv”; and just use the webpages folder.
Or, if you want the csv file to be in a folder use $filelocation=“foldername/formtest.csv”;

This may have to change if you place it online. You didn’t mention where you were testing it local or online.

Hope that helps…

I checked and though I was changing the file permissions to 777 though FTP, they were saving as 666. I tried from two different FTP accesses, and it happened on both. Any suggestions on how to get this changed? Is there a way to do it in excel?

FTP should allow you to alter a folder’s permissions. I do it all the time.
Interestingly enough, I tried to alter the attributions to 777 on one of my files and it did not allow it.
I had to log into my hosting site and alter it there. Odd! I have changed it before… ???

So, you probably will have to log into your host and alter it there… Good luck…

I have had some success. I’ve been able to get the file to open, write, and save. IT works perfectly . . . except that it isn’t writing anything. When I open the CSV file, it is just a row of commas, but no text or values. Here is the full code of the page below. I can’t for the life of me, see what could be causing the error:

[php]<?php
//this is where the creating of the csv takes place
$cvsData = $fn . “,” . $ln . “,” . $address . “,” . $city . “,” . $state . “,” . $zip . “,” . $phone . “,” . $email . “,” .$emailMe . “,” . $comments ."\n";

$fp = fopen(“formTest.csv”,“a”); // $fp is now the file pointer to file $filename

if($fp){
fwrite($fp,$cvsData); // Write information to the file
fclose($fp); // Close the file
echo “File saved successfully”;
} else {
echo “Error saving file!”;
}
?>

<?php $fn = $_POST['fn']; $ln = $_POST['ln']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $email = $_POST['email']; $emailMe = (isset($_POST['emailMe'])) ? $_POST['emailMe'] : 'No'; $comments = $_POST['comments']; //validate if(empty($fn) || empty($ln) || empty($address) || empty($city) || empty($state) || empty($zip) || empty($phone) || empty($email)){//show the form $message = 'Fill in areas in red!'; $aClass = 'errorClass'; ?>
<?=$message;?>
First Name Last Name
Address
City State ZIP
Phone Email Please send me email
Comments
<?php } ?>[/php]

yeah, i was gonna say the same thing… use cpanel from ur hosts site and change them there… erm , maybe 755 better haha… but try 777 first to see if it works… sometimes ftp progs are funny with permissions… CPANEL ALL THE WAY lol

Well, since you didn’t show us the code that where you got the variables from, we can’t help you.

The “$cvsData = $fn . “,” . $ln …” uses $fn etc. Where did you get these?
Usually they would be pulled from the database or from a form. If a form they would be $_POST[‘filename’] or some other name of a textfile that was posted…

So, unclothe your code and show it to us, or, guess… My guess is that you are not pulling the data correctly from the posted form. Either you didn’t use the variables from the form or the names used in the POST are spelled a little different. NOTE: CAPS are very important! Good luck…

The enter code was posted, include the form information. It’s pulling the variable from the form. all the caps are correct.

All enter code was posted from where?

You only showed us one file…

For a form to work, it is in a HTML file and for the action=“somefile.php” and when posted, it runs the PHP code which reads the $_POST’s and displays another page that is created from the data POSTED to it.

Does your code involve two files or are you using one page that calls itself. If so, why would you want to post data to yourself? It would not be useful data as it can never go anywhere.

Since you said you were a new PHP programmer, I will explain it a little. First, you create your form which is usually on a HTML page. This form asks for inputs of various types. Once the user is ready to submit the data, a submit button sends the data on the page to another file which is a PHP file that processes the data. The PHP file will process the data and write files or whatever needs to be done and will either echo a new page back with output from the processing or just redirect back to another page such as “processing completed…” or whatever, perhaps back to the original page so the user can select more info to process.
Anyway, posting back to itself doesn’t do much.

Does this make sense? Oh, if you are just testing and really want to just process one page, let me know and I will change your code to do so. But, that is not what PHP is really about.

One page that calls itself. I usually use a page that sends out and email, that’s all I ever use PHP for is sending an email from a form, and I have several of those that work fine. This one is just supposed to save data to a CSV file, that’s all I want it to do.

What I sent you is one web page, with three things going on. The top is the PHP that save the file. The second part is the validating part. The last part is the Body of the page with the Form.

Like I said, it is opening the CSV, saving, and closing, and you can open the CSV and see new lines of “,” for every save, but it isn’t transmitting the data from the variables.

Okay, that is because you are not posting in the correct order, I am sure.

I will put it on my server and post a new version. Give me a little while…

I guess I do not understand how to explain this…

Your code starts out attempting to write out a file with no data in it.
Next it tries send an email with POSTED data which of course does NOT exist.
Next it shows a form with POSTED data which gives it nothng to put into it’s fields.

Soooo, it just causes error after error and “renders” a blank form. If all you want to do is use one page that does nothing server-side, just use Javascript to post and email. No server needed except to display the orginal page in the browser.

You have no code to tell the PHP code NOT to work if it is the first past thru. This is totally against ALL PHP general programming rules. It just doesn’t make sense why it would be done in PHP.

If you MUST do this, then at list put a "if(isset(‘submit’) at the beginning to ignore it unless it was posted to by this page. At least then, it would be somewhat valid code.

I Tried putting the other stuff after the form, but still nothing. I’m not trying to send an email, I just want the stuff that is entered into the form saved in the CSV. I don’t think I can do that in Java.

Oh, I looked at your code, you had two sections of PHP one after another, thought the second was for emailing… Sorry…

Okay, I will piece this together to work even though it will be the worst page of PHP code I ever wrote.
Totally not a professional way to do this page. Since you do not want to use a second page for processing.
Give me a few minutes…

Never mind, this is a crap project. It makes no sense! You are joking me right?

This is what you are asking…

  1. A page loads with a nice looking form with no data. (normal)
  2. Data is entered to be appended to a CSV file (okay, still normal)
  3. The form is POSTED to itself to display the data from the preivous post (Odd, why do that it was posted)
  4. Wait for more data to be added and loop to #2 (no reason, why? Data is never used or accessed)

You MUST explain why anyone would want to do this. It just does not make any sense. If it was April 1st, I would get it…

Why do you want this data if it is not used. Why no buttons to do anything else. Why a recursive form that never does anything?

You will not get any programmer on this site to help you finish a crazy routine like this! Makes no sense!

Sponsor our Newsletter | Privacy Policy | Terms of Service