views:

289

answers:

3

Hey,

I am trying to either create a file that doesn't exist or write to a file that already does.

within a php file I am trying this:

$file = fopen("data.txt", "a");

fwrite($file, "\n" . $name); fwrite($file, "," . $lastname);

fwrite($file, "," . $email);

fclose($file);

I am running Apache under windows Xp and have no luck that the file "data.txt" is being created. The docs say that adding the a parameter should create a file with a name mentioned in the fist parameter (data.txt). what am I doing wring here?

Thanks in advance undersound

+1  A: 

Are you looking into that directory the script also considers it's current one? Does your apache process have write permission there? Does the error log mention any failing command in this?

Konrad Neuwirth
Hey Konrad Neuwirth, Thanks for your reply. Are you looking into that directory the script also considers it's current one?Dunno how the function works, I am expecting it searches the current dir How do i check if I have write permissions, I am running a localhost server, Apache, under windows XP, and don't have any experience with that. Error log / Acces log doesn't mention anything
You can get the script's current working directory with `getcwd()`. Also, you should check `if ($file === FALSE) { etc...}` after the fopen() call. If the script can't open the file for any reason, it returns false.
Marc B
A: 

Remember that the file is created in the directory where the main thread of the process is running. This meas that if the "main" program is in A/ and it includes a functions file from B/ a function in B that creates a file in "." creates it in A/ not in B/

Also, there's no need to call the fwrite function 3 times, you can do

fwrite($file, "\n" . $name . ", " . $lastname . ", " . $email);
Ben
And you can actually write that as <pre>fwrite($file,"\n$name, $lastname, $email"); </pre>
Konrad Neuwirth
+1  A: 

Let's add some tests and debug output to your code

echo 'cwd is: ', getcwd(), "<br />\n";
echo 'target should be: ', getcwd(), "/data.txt <br />\n";
echo 'file already exists: ', file_exists('data.txt') ? 'yes':'no', "<br />\n";

$file = fopen("data.txt", "a");
if ( !$file ) {
  die('fopen failed');
}
$c = fwrite($file, "\n$name,$lastname,$email");
fclose($file);
echo $c, ' bytes written';
VolkerK