Re: fwrite not writing

From: Date: Mon, 16 Apr 2001 19:06:46 +0000
Subject: Re: fwrite not writing
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-48793@lists.php.net to get a copy of this message
In article <9bejks$gl6$1@toye.p.sourceforge.net>, aemuli@spamclause-yahoo.com ("Plutarck") wrote: > I'm not sure why it suddenly stopped working, but let's see if you actually > need it to work. > > First of all, I take it that you have content which you want to write into a > file. Correct? > > If so, why does it matter if the file you want to write it to has data > anyway? If you are just going to add data into it, isn't it ok that it is a > zerolength file? It is needed. I have a dynamically generated page. Most of the content needs to be re-generated with every request. But some of it needs to be re-generated far less often. Rather than unnecessarily hit the db every time to get the exact same content, that content gets cached to an include file. This function checks whether that file needs to get a content refresh, and if so rewrites that content. (I'm not adding data to the file, I'm overwriting it.) > Or is the problem that the file is _not_ actually zerolength, but PHP says > it's zerolength anyway? Nope, it's really zero length. The filesystem agrees with PHP, and when I open it in a text editor the file is empty. > Just try removing the !filesize() part, and then try executing the function. I commented out the entire if() condition, it makes no difference. Maybe I have not made this clear enough: the problem is not in getting inside the loop. The function *is* executing the loop, and *is* reaching the fwite() line as well as continuing without error past that line. The frustrating part is that fwrite() is writing an empty string to the file instead of using the $content variable (which, when echoed immediately before that line, is definitely not empty/null). > But maybe I just don't understand what you are trying to write and why. > Could you be a little more specific what data is being saved in the file, > and what is in the file already? Sometimes the file has data, sometimes it doesn't; sometimes the file exists, sometimes it does. Right now, unfortunately, the file remains empty. I'm trying to fill it with a large block of dynamically generated HTML which will then be used as a static include file for current and subsequent requests. > "CC Zona" <cczona@nospam.invalid> wrote in message > news:9bebi3$133$1@toye.p.sourceforge.net... > > > > This function suddenly stopped working, and I just can't seem to > figure > > > out > > > > why. The only change made recently is that now the value of $force at > > > > calltime is sometimes true instead of being undefined or null. > > > > > > > > > > > > build_file("file_content","/path/to/file.inc","w",TRUE); > > > > > > > > function build_file($func_name,$filepath,$mode="w",$force=FALSE) > > > > { > > > > if($force or !file_exists($filepath) or !filesize($filepath)) > //echo > > > > filesize($filepath) shows '0' > > > > { > > > > $content=$func_name(); //echo $content shows it's all there > > > > $fp=fopen($filepath,$mode); > > > > fwrite($fp,$content); > > > > rewind($fp); #temp test > > > > $read_back=fread($fp,100000); #temp test > > > > echo "<p>file content:</p>\n $read_back"; #temp > > > > test, displays > > > nothing > > > > fclose($fp); > > > > } > > > > } > > > > > > > > I've tried putting echoes and "or die('Error on > > > > __LINE__')" on every > line, > > > > checked all the variable values, and found no answers from that. > > > > Everything shows exactly as it should be except that the content that > > > > echoes out so nicely *doesn't ever get written to the file*. The > function > > > > runs to the end without error and the file's modification date is even > > > > updated. But the file remains empty. I'm probably missing something > > > > ridiculously obvious, but would someone please be kind enough to point > out > > > > what it is? Thank you!! > > > > > What is this: > > > > > > !filesize($filepath) > > > > If filesize is zero (which it is <groan>), then do the rest (which it > > does--except the content it fetches never makes it into the file it writes > > to. How that can be, I dunno, but that apparently is what it's doing...) > > > > > Add this above your if loop: > > > > > > $filesize = filesize($filepath); > > > echo $filesize; > > > > Already tried echoing that and all the other values. Filesize is 0. > > > > > That might be causing your loop not to execute...if not, I'm not sure > what's > > > wrong. > > > > I don't get it. It should work. It did work. Suddenly it's not. -- CC

« previous php.general (#48793) next »