...

View Full Version : Warning: fclose(): supplied argument is not a valid stream resource



ska_defender
08-04-2012, 02:06 PM
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);

Fou-Lu
08-04-2012, 05:32 PM
You should also have an error on the fwrite. fclose should only be throwing that if $fp isn't a valid stream, which indicates that your fopen has also failed.
Use an if to determine if continuation is possible:


if ($fp = fopen('data.txt', 'w'))
{
fwrite($fp, 2);
fclose($fp);
}

ska_defender
08-05-2012, 01:43 PM
This is the full 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...';

AndrewGSW
08-05-2012, 02:27 PM
I don't know what the issue is, but concerning 'fread'


If you just want to get the contents of a file into a string, use file_get_contents() as it has much better performance

Perhaps try file_get_contents() instead.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum