Page 1 of 1
Write Errors
Posted: Thu Sep 18, 2003 3:15 am
by shirokuma
I have a script that writes the result of a query to a text file. I run the script from the command line by invoking it like pgp.exe -f query.php. The code runs over and over again but sometimes PHP fails to open a file. I get error messages like the following.
Code: Select all
<b>Warning</b>: fopen(d:\logs\reports\html\sec\today\temp\info.txt): failed to open stream: No such file or direct
ory in <b>C:\mimi\query.php</b> on line <b>205</b><br />
<br />
<b>Warning</b>: fwrite(): supplied argument is not a valid stream resource in <b>C:\mimi\query.php</b> on line <b>208</
b><br />
.
.
.
fwrite($outputfp,"<table border=1>\n<tr>\n");
The code I use to open the file is like this:
Code: Select all
$tempfile = $report_dir.""e;.$output_file;
$outputfp = fopen($tempfile, "a");
$report_dir and $output_file are defined elsewhere prior to these two statements. Does anyone know why I might be getting intermittent errors?
Your feedback is greatly appreciated.
Shirokuma
Posted: Sun Sep 21, 2003 5:30 am
by tylerdurden
Sometimes it works and sometimes it doesn't? Maybe the file is open in another process? This sometimes is a problem under Windows. Do you use fclose() after the write?
Write Errors
Posted: Sun Sep 21, 2003 8:19 pm
by shirokuma
I always use the fclose() statement when I'm done. However, the problem is occuring the first time I try to write something to the file. It just isn't opened. What you say about the file being in use by another process might be the case.
What happens is that I have a DOS batch script that sends a series of parameters to the PHP script over and over again. The PHP script, query.php, takes the parameters and does a search in my query_tbl table. This holds a list of queries for each key. There are 10 to 30 queries for each key. query.php runs each found query against another table. If any results are found they are written to one or more temporary files. For each set of queries new temporary files are created so that the data from one set of queries doesn't get mixed up with data from the previous. The problem occurs that first time the script tries to write to the new temporary file.
There are somewhere around 100 keys. During this past 3-day weekend the error occured 26 times on 9/20, 4 times on 9/22 and not at all on 9/21. However, on 9/21 something happened to foul up almost the entire run of the script. The error doesn't occur for the same key everytime. I have this script running on three different machines. The errors seem to occur more frequently on the older, slower machines.
TIA,
Shirokuma
Posted: Mon Sep 22, 2003 2:14 am
by tylerdurden
Hard to tell but it looks like the script is stumbling over it's own feet or over the filesystem's. Maybe you could try inserting a pause (sleep(2)) after each query to make sure everything's cleaned up. Only a guess though. You could also look into
flock(). To lock your file while still working on it.[/url]
Write Errors
Posted: Thu Sep 25, 2003 2:49 am
by shirokuma
I tried using the sleep() function but I got the same errors. I have been watching the results of my daily script run more carefully and I have noticed that the error occurs more frequently with certain parameters. Why this should be I have no idea since this has nothing to do with where the temp files are written to.
I think that tylerdurden's idea that it is something going on with the filesystem and not the script itself that is causing the problem. I have another, older server which the same script runs on and the error occurs more frequently on that server than on my newer, high spec server.
I read the manual entry for flock(). This won't work because my server is a Windows 2000 system.
Does anyone else have any thoughts on this problem?
Shirokuma