Forum Moderators: coopster

Message Too Old, No Replies

Problem using fwrite

         

kevinashby

7:18 pm on Oct 16, 2010 (gmt 0)

10+ Year Member



Hi all,

I have a script that just about works.

I need to write 2 items from a form. The form posts to this script:


<?php

$filename = "filename.csv";
$string = '"'.$_POST['id'].'","'.$_POST['email'].'" \n';
if (file_exists($filename)) {
$file = fopen($filename, "a");
fwrite($file, $string);
} else {
$file = fopen($filename, "a");
fwrite($file, '"id","Email" \n');
fwrite($file, $string);
}
fclose($file);

header( "Location: www.my-domain.com/success" );

?>



This is what the output should look like in the CSV file:

1 , email-1@somedomain.com
2 , email-2@somedomain.com
3 , email-3@somedomain.com

This is what the output is at the moment:

1 , test-1@test.com"2 , test-2@test.com


If I use this script without the form and change the string to something like:

$string = 'This is what I want inserted into the file \n';


Then it works just fine. What am I missing?

Please help if you can. Thanks


Great news, just try to do things a little differently and they work (silly me).

For anyone who is looking for a solution to the same probelm:

<?php
$id = $_POST['id'];
$email = $_POST['email'];
$fp = fopen("filename.csv", "a");
$savestring = $id . "," . $email . "\n";
fwrite($fp, $savestring);
fclose($fp);
echo "Your data has been saved!";
?>


<Edited by owner. Reason - solution found>

Matthew1980

7:37 pm on Oct 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there kevinashby,

I have looked at your code, and there is a syntax error in the second fwrite (which i assume is the fail safe as it only writes "id, email" to the chosen file?)

I have added a error handler to this now, hopefully it will do as you want it too:-

<?php

$filename = "filename.csv";
$string = $_POST['id'].",".$_POST['email']."\n\r';<--Concatenated your vars properly here

if(file_exists($filename)){
$file = fopen($filename, 'a');
$success = fwrite($file, $string);

//Handle a failed write here
if($success == FALSE){
echo "something went wrong writing to file";
exit;
}
}
else{
//this part means that the file wasnt there so
//you can't write to it?
$file = fopen($filename, "a");
fwrite($file, "id, Email\n\r");<--Syntax error was here
fwrite($file, $string);
}

//close file handle
fclose($file);

header("Location: www.my-domain.com/success");
//kill the script after header, goo practise to do this ;)
exit;

?>


You could do with adding some sanitising your $_POST vars, as potentially you could leave yourself open to abuse if your intending to read from this and parse it to send lots of emails..

But I think as you need to check your logic in this, because your checking to see if the file_exists() first, using the if(), the else clause then attempts to open that file anyway, which, the last time I looked, means that it would fail. This is unless I have read it totally wrong.

Personally I would just add a preg_match() filter for validating email address formats.

Hope you can see what I am trying to convey anyway.

Cheers,
MRb

kevinashby

4:17 am on Oct 17, 2010 (gmt 0)

10+ Year Member



Hi MRb,

Thanks for that.

I found a really simple way to do this which I added to the bottom of my initial post (but no sanitising as it is just a simple item for in-house use - will PW protect the folder though).