1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Php Error Help "warning: Fwrite(): Supplied Argument Is Not A Valid Stream Resource "

Discussion in 'PHP' started by saturn100, Jan 24, 2013.

  1. #1
    Hi
    I have a site using a cms script
    I have used the script on other sites but having issues with one site
    The site is loading and working but I am getting the following errors at the top

    Warning: fwrite(): supplied argument is not a valid stream resource in /mounted-storage/.../stat.php on line 5

    Warning: fclose(): supplied argument is not a valid stream resource in /mounted-storage/.../stat.php on line 5

    The code it is refering to is
     { @$fp = fopen("$AdminFolder/stat/stat.dat","r"); $dt = fgets($fp,255); $st = explode("|",$dt); fclose($fp); $id = $st[$n]; $idtt = $st[0];if ($id=="") $id = 0; $id++; if ($idtt=="") $idtt = 0; $idtt++; if ($day != $st[8]) {$id = 1;}$st[$n] = $id; $data = $idtt.'|'.$st[1].'|'.$st[2].'|'.$st[3].'|'.$st[4].'|'.$st[5].'|'.$st[6].'|'.$st[7].'|'.$day.'|'; }
      else {$data = '0|0|0|0|0|0|0|0|0|';} @$fp = fopen("$AdminFolder/stat/stat.dat","w"); fwrite($fp, $data); fclose($fp); } ?>
    
    PHP:
    I can't figure out what the problem is
    I havent edited the file at all

    Is there a way to put in the actual route of the folder in question instead of a php command

    Thanks
     
    saturn100, Jan 24, 2013 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    The server does not have access to the file, either because of permissions or paths. Create the directory it's supposed to reside in and grant the php process and or server write permissions. Not a valid stream resource means fopen failed to open the file on disk ...
     
    krakjoe, Jan 24, 2013 IP
  3. GORF

    GORF Well-Known Member

    Messages:
    224
    Likes Received:
    21
    Best Answers:
    3
    Trophy Points:
    165
    #3
    It looks like you are missing brackets in your if statements.
     
    GORF, Jan 24, 2013 IP
  4. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #4
    well as i said i didnt edit will have a look now
    the warings go if I refresh the page for some reason
     
    saturn100, Jan 24, 2013 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Oh, I think I can explain this, your modern browser is opening two connections and you are trying to write the file from two requests at once, it's a race condition I think ...

    Think about using file locking, or a more suitable file format for your database, I'm surprised you've never seen the errors before ....

    Also you should think it odd that programmers cannot detect if you should have brackets or braces in your code, while asking for help it's a good idea to post properly formatted code by the file or blockfull, we should quite easily be able to detect syntax errors ...
     
    krakjoe, Jan 24, 2013 IP
  6. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #6
    <?php
    $d = date("D"); $dd = time(); $j = array( "Mon" => 1, "Tue" => 2, "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6, "Sun" => 7);
    $n = $j[$d]; $day = date("d"); if(!isset($_COOKIE['stat'])) { setcookie("stat", time(), time()+1200); if (file_exists("$AdminFolder/stat/stat.dat"))
      { @$fp = fopen("$AdminFolder/stat/stat.dat","r"); $dt = fgets($fp,255); $st = explode("|",$dt); fclose($fp); $id = $st[$n]; $idtt = $st[0];if ($id=="") $id = 0; $id++; if ($idtt=="") $idtt = 0; $idtt++; if ($day != $st[8]) {$id = 1;}$st[$n] = $id; $data = $idtt.'|'.$st[1].'|'.$st[2].'|'.$st[3].'|'.$st[4].'|'.$st[5].'|'.$st[6].'|'.$st[7].'|'.$day.'|'; }
      else {$data = '0|0|0|0|0|0|0|0|0|';} @$fp = fopen("$AdminFolder/stat/stat.dat","w"); fwrite($fp, $data); fclose($fp); } ?>
    
    PHP:
    Thats the full code
    If I replace "$AdminFolder/stat/stat.dat" with the actual address would that help or is that a really bad idea
     
    saturn100, Jan 24, 2013 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    
     
    <?php
    $d = date("D"); $dd = time(); 
    $j = array( "Mon" => 1, "Tue" => 2, "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6, "Sun" => 7);
    $n = $j[$d]; 
    $day = date("d"); 
     
    if (!isset($_COOKIE['stat'])) { 
       setcookie("stat", time(), time()+1200);
     
       if (file_exists("$AdminFolder/stat/stat.dat")) { 
     
           $fp = @fopen("$AdminFolder/stat/stat.dat","r"); 
           if ($fp) {
               $dt = fgets($fp,255); 
               $st = explode("|",$dt); 
               fclose($fp); 
               
               $id = $st[$n]; 
               $idtt = $st[0];
     
               if ($id=="") 
                   $id = 0; $id++; 
               if ($idtt=="") 
                   $idtt = 0; $idtt++; 
               if ($day != $st[8]) {
                   $id = 1;
               }
     
               $st[$n] = $id; 
               $data = $idtt.'|'.$st[1].'|'.$st[2].'|'.$st[3].'|'.$st[4].'|'.$st[5].'|'.$st[6].'|'.$st[7].'|'.$day.'|';
           }
     
       } else {
           $data = '0|0|0|0|0|0|0|0|0|';
       } 
     
       $fp = @fopen("$AdminFolder/stat/stat.dat","w"); 
       if ($fp) {
           fwrite($fp, $data); 
           fclose($fp);
       }
    } 
    ?>
    
    PHP:

    This will not show or throw errors, but nor will it result in useable stats on a busy site either. If 100 people come along, and they all write the same file at once, some of them will write over what other instances are writing and your data will become corrupted. A flat file database like this is never a good idea, unless infront of the file is a service that manages access, or the protocol used to access the file has built in mechanism to stop corruption, like sqlite. You can see it causing you a problem when just two connections are opened, it's definitely not a good idea.
     
    krakjoe, Jan 24, 2013 IP
  8. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #8
    What about the basics:

    $filename = "$AdminFolder/stat/stat.dat";
    $fp = fopen($filename, "w") or die ("Couldn't Open $filename");

     
    ezprint2008, Jan 28, 2013 IP