0

I want to write something in a textfile.

$myFile = "meinung.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopy\n";
fwrite($fh, $stringData);
$stringData = "Pointy Pinto\n";
fwrite($fh, $stringData);
fclose($fh);

when i want to execute this, it says "cant open file"

what did i make wrong?

3
  • 3
    Do you have write permissions in that directory? Commented Apr 6, 2011 at 9:22
  • 3
    Do you have permissions to write to the file/folder ? Commented Apr 6, 2011 at 9:22
  • You could try taking out the die and seeing what error it is giving you. Commented Apr 6, 2011 at 9:30

1 Answer 1

1

Place

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
...

on top of your code to get error messages on screen or in your logfile (You know where your error logfile is, don't you?).

Set an absolute path to your file so that you really know in what directory the file is actually created to fix the filesystem permissions as noted in the comments.

Sign up to request clarification or add additional context in comments.

3 Comments

Addition: With $myFile = dirname(__FILE__) . '/meinung.txt; (or $myFile = __DIR__ . '/meinung.txt'; since PHP 5.3) you'll get the absolute path.
@KingCrunch - __FILE__ (with absolute path) is available since PHP 4.0.2; and: maybe he wants to place the content to some other directory than that of the script.
I know __FILE__ is available since ... for a long time ^^ __DIR__ is available since 5.3. Thats the reason you must use something like dirname(__FILE__) in versions <5.3. The hint with the directory is valid. So $myFile = getcwd() . '/meinung.txt'; is probably better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.