fclose Warning: fclose(): supplied argument is not a valid stream resource
17,128
Solution 1
When your if() condition returnes false, there's no $fp file opened so there's nothing to close. PHP does not understand what file you wanted to close cuz there's nothing opened.
Solution 2
Your problem is in the "if-else" statement (file not opened)... check your code
Author by
user1561466
Updated on June 14, 2022Comments
-
user1561466 almost 2 years
I am getting the warning on my pages
Warning: fclose(): supplied argument is not a valid stream resource
Using the following code
$fp = fopen('data.txt', 'w'); $write = '2'; fwrite($fp, $write); fclose($fp);
Update code
if(isset($_REQUEST['go1'])) { $fp = fopen('data.txt', 'w'); $write = '1'; $fp1 = fopen('file.php', 'w'); $write1 = '<br><img src="/1/online.png" style="position:absolute; z-index:-2;" />'; } if(isset($_REQUEST['go2'])) { $fp = fopen('data.txt', 'w'); $write = '2'; $fp1 = fopen('file.php', 'w'); $write1 = '<br><img src="/1/offline.png" style="position:absolute; z-index:-2;" />'; } fwrite($fp, $write); fclose($fp); fwrite($fp1, $write1); fclose($fp1); $fp = fopen('data.txt', 'r'); $contents = fread($fp, filesize('data.txt')); fclose($fp); if($contents == '1') include('file.php'); else if($contents == '2') include('file.php'); else echo 'Something else...';
getting the error on line 27 and line 29 line 27
fclose($fp);
line29
fclose($fp1);
-
Burhan Khalid almost 12 yearsDid you check to make sure the file was opened correctly?
-
user1561466 almost 12 years@Burhan Yes the file is opening and writing the same
-
Vatev almost 12 yearsIs there anything else in the script? If there is, try to simulate the same thing with only these lines. Also, make sure 'data.txt' is actually written to and not just some old file from previous experiments :)
-
user1561466 almost 12 years@Vatev I have updated the code,Please advise
-
-
user1561466 almost 12 yearsbut the file is opening and writing the same
-
paxdiablo almost 12 yearsYou should still check the return code from
fopen
(andfwrite
by the way) just in case. Permissions are one reason for failure, there could be others. Otherwise please confirm that the code you have posted is exactly what you have (including with no intervening stuff). The reason I ask is that your code is basically the canonical example as per php.net/manual/en/function.fwrite.php, so it's unlikely it wouldn't work as is. -
paxdiablo almost 12 years@user1561466: there is a code path where you don't open the file but attempt to close it - see the update.
-
user1561466 almost 12 yearsI have checked the written file and its confirm that the file is opening and desire code is written to it