Home  >  Q&A  >  body text

php fopen() failed to open file

$fild='./text.txt';

$userdata = fopen($fild,'ab');

if ($userdata==false){

echo 'Failed to open file';

}else{

echo 'Failed to open file';

$rel = fwrite($userdata,"aaaaa" );

fclose($userdata);

}

There is no problem with the code. In phpstorm’s own running environment http://localhost:63342, the file can be opened normally and write. But in http://localhost/, the file cannot be opened and written, and "Failed to open file" is displayed.

How to solve it?


Ader.WuAder.Wu2643 days ago1511

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-08-18 09:10:29

    You can use file_get_contents and file_put_contents to read and write files, which is easier. It is also possible that the file does not have read permission. Try changing the second parameter of fopen to w+.

    reply
    0
  • Cancelreply