$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?
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+.