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
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 ...
well as i said i didnt edit will have a look now the warings go if I refresh the page for some reason
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 ...
<?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
<?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.
What about the basics: $filename = "$AdminFolder/stat/stat.dat"; $fp = fopen($filename, "w") or die ("Couldn't Open $filename");