Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    I am getting the warning on my pages
    Warning: fclose(): supplied argument is not a valid stream resource

    Using the following code:
    PHP Code:
    $fp fopen('data.txt''w');
    $write '2';
    fwrite($fp$write);
    fclose($fp); 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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:
    PHP Code:
    if ($fp fopen('data.txt''w'))
    {
        
    fwrite($fp2);
        
    fclose($fp);


  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This is the full code

    PHP 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($fpfilesize('data.txt'));
    fclose($fp);
    if(
    $contents == '1')
    include(
    'file.php');
    else if(
    $contents == '2')
    include(
    'file.php');
    else
    echo 
    'Something else...'

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •