Massive problem with output buffering
Massive problem with output buffering
am 21.11.2007 21:57:27 von jools
I'm having trouble modifying some code written by someone else.
The code is very dense and obscure but does work fine. However I need to
insert a block of my own and I've hit what I assume is a buffering
problem.(If there's another answer I'd be grateful ...)
The problem is PHP is refusing to do things in the correct sequence.
I've tried all the variations of flushing I can think of without success.
Can anyone suggest how I can force PHP to do this task in the correct
sequence please?
Thanks
-jools-
Using PHP 5.1
Centos Linux V4
Apache 2 server
I need to do this:
------------------
1. I create a text file on the fly inside a folder
2. I output some lines from that text file -(May include some HTML such
as a form but is not activated/posted) using echo or print
3. I delete the text file from the drive
The sequence that occurs however is not 1 2 3
but 1 3 2
And I get an error because the file can't be found by event 2
OK I thought - I remember something about ob_flush so I tried
1
2
ob_flush(); // or a variant such as ob_end_flush()
3
And still I get 1 3 2 instead of 1 2 3
I've tried ob_start
i.e
1
ob_start()
2
ob_end_flush()
3
but no luck
I've tried multiple ob_flush()'s one after the other - no luck
-----------------------------
Re: Massive problem with output buffering
am 21.11.2007 22:07:59 von luiheidsgoeroe
On Wed, 21 Nov 2007 21:57:27 +0100, wrote:
> I'm having trouble modifying some code written by someone else.
> The code is very dense and obscure but does work fine. However I need to
> insert a block of my own and I've hit what I assume is a buffering
> problem.(If there's another answer I'd be grateful ...)
>
> The problem is PHP is refusing to do things in the correct sequence.
>
> I've tried all the variations of flushing I can think of without success.
>
> Can anyone suggest how I can force PHP to do this task in the correct
> sequence please?
>
> Thanks
> -jools-
>
> Using PHP 5.1
> Centos Linux V4
> Apache 2 server
>
> I need to do this:
> ------------------
>
> 1. I create a text file on the fly inside a folder
> 2. I output some lines from that text file -(May include some HTML such
> as a form but is not activated/posted) using echo or print
> 3. I delete the text file from the drive
>
> The sequence that occurs however is not 1 2 3
> but 1 3 2
> And I get an error because the file can't be found by event 2
>
> OK I thought - I remember something about ob_flush so I tried
This has nothing to do with output buffering. Everything you echo/print
will either be send directly or keep hanging in a buffer, totally
unrelated to the text file. If you've send them (to the buffer), there's
no way deleting the file can alter anything. If it's a buffer of PHP
itself, and that buffer gets flushed/deleted it's another thing.
So, your problem remains: why is is 1,3,2 and not 1,2,3? Impossible to
tell without any code. You could use some debugging/profiling pacckages to
help you find the 'real' flow through the code, or try to copy the
codebase to another location, and trim it rigurously down untill you have
a minimal code example with the mentioned unexpected behaviour.
--
Rik Wasmus
Re: Massive problem with output buffering
am 21.11.2007 22:12:59 von jools
In article , luiheidsgoeroe@hotmail.com
says...
> through the code, or try to copy the
> codebase to another location, and trim it rigurously down untill you have
> a minimal code example with the mentioned unexpected behaviour.
>
>
Good Idea Rik - I'll do that and post an example here. I just dont
understand how this can happen at the moment.
thanks
-jools-
Re: Massive problem with output buffering
am 21.11.2007 22:13:29 von Jerry Stuckle
jools@by.com wrote:
> I'm having trouble modifying some code written by someone else.
> The code is very dense and obscure but does work fine. However I need to
> insert a block of my own and I've hit what I assume is a buffering
> problem.(If there's another answer I'd be grateful ...)
>
> The problem is PHP is refusing to do things in the correct sequence.
>
> I've tried all the variations of flushing I can think of without success.
>
> Can anyone suggest how I can force PHP to do this task in the correct
> sequence please?
>
> Thanks
> -jools-
>
> Using PHP 5.1
> Centos Linux V4
> Apache 2 server
>
> I need to do this:
> ------------------
>
> 1. I create a text file on the fly inside a folder
> 2. I output some lines from that text file -(May include some HTML such
> as a form but is not activated/posted) using echo or print
> 3. I delete the text file from the drive
>
> The sequence that occurs however is not 1 2 3
> but 1 3 2
> And I get an error because the file can't be found by event 2
>
> OK I thought - I remember something about ob_flush so I tried
>
> 1
> 2
> ob_flush(); // or a variant such as ob_end_flush()
> 3
>
> And still I get 1 3 2 instead of 1 2 3
>
> I've tried ob_start
> i.e
>
> 1
> ob_start()
> 2
> ob_end_flush()
> 3
> but no luck
>
> I've tried multiple ob_flush()'s one after the other - no luck
>
> -----------------------------
>
>
>
>
In addition to what Rik said, look at the source for the page. Did it
come out in the right order? If so, your html is bad (wouldn't be a bad
idea to validate the html anyway).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Massive problem with output buffering
am 21.11.2007 22:15:10 von jools
> If it's a buffer of PHP
> itself, and that buffer gets flushed/deleted it's another thing.
While I'm doing that could you explain this a little please? It sounds
like it could be possible but I dont know enough about how PHP works
What buffer might it use - or could you point me at the right place in
the PHP docs ?
thanks
-jools-
Re: Massive problem with output buffering
am 21.11.2007 22:19:12 von jools
Just so you dont all think I'm completely mad - if I just do this
// unlink('text.txt');
and leave the text file on the drive - the output at sequence 2 is
suddenly correct.
(I have tried clearstatcache(); at various places too...)
I'll post the sample as soon as I've cut it down.
thanks
-jools-
Re: Massive problem with output buffering
am 21.11.2007 22:39:35 von luiheidsgoeroe
On Wed, 21 Nov 2007 22:15:10 +0100, wrote:
>
>> If it's a buffer of PHP
>> itself, and that buffer gets flushed/deleted it's another thing.
>
> While I'm doing that could you explain this a little please? It sounds
> like it could be possible but I dont know enough about how PHP works
If you get an error that your file is not readable, this would not be the
problem. The reading should allready be done, before a possible buffer may
or may not be discarded. Would this be the case you mysteriously wouldn't
have any content without any error.
> What buffer might it use - or could you point me at the right place in
> the PHP docs ?
Buffers can exists anywhere between the requester and the PHP script
(PHP,webserver,proxy,router,UA, etc.). See for default PHP buffers
. What different buffers
are active at any point in the code can be checked with
ob_get_status(true);
--
Rik Wasmus
Re: Massive problem with output buffering
am 21.11.2007 23:42:49 von Courtney
jools@by.com wrote:
> I'm having trouble modifying some code written by someone else.
> The code is very dense and obscure but does work fine. However I need to
> insert a block of my own and I've hit what I assume is a buffering
> problem.(If there's another answer I'd be grateful ...)
>
> The problem is PHP is refusing to do things in the correct sequence.
>
> I've tried all the variations of flushing I can think of without success.
>
> Can anyone suggest how I can force PHP to do this task in the correct
> sequence please?
>
> Thanks
> -jools-
>
> Using PHP 5.1
> Centos Linux V4
> Apache 2 server
>
> I need to do this:
> ------------------
>
> 1. I create a text file on the fly inside a folder
> 2. I output some lines from that text file -(May include some HTML such
> as a form but is not activated/posted) using echo or print
> 3. I delete the text file from the drive
>
> The sequence that occurs however is not 1 2 3
> but 1 3 2
> And I get an error because the file can't be found by event 2
>
I had a similar experience. PHP does NOT flush a file to disk it seems
until the program exits, or you do in my case a COPY() on it.
My workaround was to create the 'file'
copy the 'file'
do a Mysql LOAD on it
unlink the original 'file' and the copy file..
> OK I thought - I remember something about ob_flush so I tried
>
> 1
> 2
> ob_flush(); // or a variant such as ob_end_flush()
> 3
>
> And still I get 1 3 2 instead of 1 2 3
>
> I've tried ob_start
> i.e
>
> 1
> ob_start()
> 2
> ob_end_flush()
> 3
> but no luck
>
> I've tried multiple ob_flush()'s one after the other - no luck
>
> -----------------------------
>
>
Did you fclose() the file first?
>
Re: Massive problem with output buffering
am 21.11.2007 23:47:59 von Courtney
jools@by.com wrote:
> Just so you dont all think I'm completely mad - if I just do this
>
> // unlink('text.txt');
>
> and leave the text file on the drive - the output at sequence 2 is
> suddenly correct.
> (I have tried clearstatcache(); at various places too...)
>
> I'll post the sample as soon as I've cut it down.
>
> thanks
> -jools-
>
You should be doing fopen(), fwrite(), fread(),fclose() & unlink ().
In this case I doubt the file will ever appear on any disk at all, but
it should be consistent within php itself.
If as I discovered, you want to do something outside PHP with it. its
fopen(), fwrite(), fclose(), copy(), exec [do something else], unlink(),
unlink(copy);
Re: Massive problem with output buffering
am 22.11.2007 00:50:46 von jools
No guys thanks but its nothing to do with not closing files or stuff like
that.
OK - good and bad news...
First my debug code was responding with a wrong error line - it isnt the
text file thats missing its the graphic () line after it.
Thats the good news...
So I've done as Rik suggested and removed all my code then replaced it
with the minimal code that causes the problem and removed the faulty
debug code. So I'm now working in a plain text editor.
/// Simplest instance of the problem ...
First - these three lines are cut/paste from my actual file -
$mypath = '../foot.gif';
echo("");
unlink($mypath);
What happens is the output says 'File missing' and never displays the
foot.gif and foot.gif has been deleted.
/// end
After looking into this I've discovered that if I do :
$mypath = 'foot.gif';
echo("");
unlink($mypath);
And put the image in that directory - it displays the image fine before
correctly deleting it.
I looked at what was happening and the CWD is the only one it works in.
If the image is in any other directory a relative link used by echo() or
print() causes it to not be found but the unlink() DOES find and delete
it! Confusing or what!
SO .. it looks like my problem is a discrepancy between PHP's CWD and a
relative path when using images but not with text files ... as I tried
a simple text file readback and it worked fine with the ../ version.
I think ... so have I missunderstood how PHP uses echo() or something
else fundamental ... or is there a problem here?
And what on earth do I do without changing the CWD?
thanks
-jools-
Re: Massive problem with output buffering
am 22.11.2007 00:53:27 von jools
In article <1195684967.32596.0@proxy00.news.clara.net>, a@b.c says...
> I had a similar experience. PHP does NOT flush a file to disk it seems
> until the program exits, or you do in my case a COPY() on it.
>
I dont know if this will help you but the clearstatcache() function
sounds like the official way to deal with that problem - if I understand
what you are saying correctly.
-jools-
Re: Massive problem with output buffering
am 22.11.2007 00:57:39 von luiheidsgoeroe
On Thu, 22 Nov 2007 00:50:46 +0100, wrote:
> No guys thanks but its nothing to do with not closing files or stuff l=
ike
> that.
>
> OK - good and bad news...
>
> First my debug code was responding with a wrong error line - it isnt t=
he
> text file thats missing its the graphic () line after it.
>
> Thats the good news...
>
> So I've done as Rik suggested and removed all my code then replaced it=
> with the minimal code that causes the problem and removed the faulty
> debug code. So I'm now working in a plain text editor.
>
> /// Simplest instance of the problem ...
>
> First - these three lines are cut/paste from my actual file -
>
> $mypath =3D '../foot.gif';
> echo("");
> unlink($mypath);
>
> What happens is the output says 'File missing' and never displays the
> foot.gif and foot.gif has been deleted.
Indeed. You said you did output some filecontents, not the name of a fil=
e. =
In this case, there's practically no way the user wil ever be able to =
download the image. An HTML tag is NOT sending the image, it's just =
telling the browser where it can be fetched.
If you need an dynamically created image displayed only once, and not =
saved permanently as a file, point the HTML src attribute to a PHP scrip=
t, =
possibly with some GET variables, and let that script output an image, =
using the GD or ImageMagick libraries for instance.
-- =
Rik Wasmus
Re: Massive problem with output buffering
am 22.11.2007 01:19:05 von jools
In article , luiheidsgoeroe@hotmail.com
says...
You said you did output some filecontents, not the name of a fil=
> e. =
>
> In this case, there's practically no way the user wil ever be able to =
>
> download the image. An HTML tag is NOT sending the image, it's just =
>
> telling the browser where it can be fetched.
>
> If you need an dynamically created image displayed only once, and not =
>
> saved permanently as a file, point the HTML src attribute to a PHP scrip=
> t, =
>
> possibly with some GET variables, and let that script output an image, =
>
> using the GD or ImageMagick libraries for instance.
> -- =
>
> Rik Wasmus
No Rik thats not correct - my fault - The image is not dynamically
created its a file on the disk that is delete after use. It was the text
file I created.
Consider - these are the cases:
If the link is not relative to the CWD the image is sent fine and deleted
afterwards. Everything works as I want it to. (Except I need the images
elsewhere)
If the image is at a relative location to CWD and the image is deleted
after it should have already been sent (or GET-ed if you like) - it is
never seen.
If it is at that relative location - and NOT deleted it is sent fine.
The only thing that has changed between working and not working is the
image location from . to ../ or any other relative location.
This is why I'm still confused.
thanks
-jools-
Re: Massive problem with output buffering
am 22.11.2007 01:36:45 von luiheidsgoeroe
On Thu, 22 Nov 2007 01:19:05 +0100, wrote:
> In article , luiheidsgoeroe@hotmail.c=
om
> says...
> You said you did output some filecontents, not the name of a fil=3D
>> e. =3D
>>
>> In this case, there's practically no way the user wil ever be able to=
=3D
>>
>> download the image. An HTML tag is NOT sending the image, it's just =
=3D
>>
>> telling the browser where it can be fetched.
>>
>> If you need an dynamically created image displayed only once, and not=
=3D
>>
>> saved permanently as a file, point the HTML src attribute to a PHP =
>> scrip=3D
>> t, =3D
>>
>> possibly with some GET variables, and let that script output an image=
, =
>> =3D
>>
>> using the GD or ImageMagick libraries for instance.
>> -- =3D
Hmm, Gravity troubles?
> No Rik thats not correct - my fault - The image is not dynamically
> created its a file on the disk that is delete after use.
Euhm, I assume it's deleted by another request later on? Can't hardly be=
=
deleted in the current request with a subrequest for the image yet to co=
me?
> It was the text
> file I created.
And the rest of the reply is about an image. Now I'm confused :). What =
happens file-wise in your request? What gets created, what gets deleted,=
=
when, and why.
> Consider - these are the cases:
> If the link is not relative to the CWD the image is sent fine
Where are you sending an image? Not in this code. This code only tells a=
=
browser where an image can be fetched in the future...
> and deleted
> afterwards. Everything works as I want it to. (Except I need the imag=
es
> elsewhere)
>
> If the image is at a relative location to CWD and the image is deleted=
> after it should have already been sent (or GET-ed if you like) - it is=
> never seen.
As I would expect.
> The only thing that has changed between working and not working is the=
> image location from . to ../ or any other relative location.
I'd reexamine some things in you code carefully:
1. Is an image actually creted, and where? Change the filename to =
something unique you haven't used before (uniqid()?), and check for it =
(remove the unlink() temporarily).
2. Is the HTML src attribute actually pointing to that image, or perhaps=
=
an image in another directory with the same name?
3. Is an image actually deleted? (reintroduce the unlink(), check wether=
=
the file actually disappears.
-- =
Rik Wasmus
Re: Massive problem with output buffering
am 22.11.2007 03:30:17 von Courtney
jools@by.com wrote:
> In article <1195684967.32596.0@proxy00.news.clara.net>, a@b.c says...
>> I had a similar experience. PHP does NOT flush a file to disk it seems
>> until the program exits, or you do in my case a COPY() on it.
>>
>
> I dont know if this will help you but the clearstatcache() function
> sounds like the official way to deal with that problem - if I understand
> what you are saying correctly.
>
If I get a chance I'll try that.
> -jools-
Re: Massive problem with output buffering
am 22.11.2007 05:23:18 von Jerry Stuckle
jools@by.com wrote:
> In article , luiheidsgoeroe@hotmail.com
> says...
> You said you did output some filecontents, not the name of a fil=
>> e. =
>>
>> In this case, there's practically no way the user wil ever be able to =
>>
>> download the image. An HTML tag is NOT sending the image, it's just =
>>
>> telling the browser where it can be fetched.
>>
>> If you need an dynamically created image displayed only once, and not =
>>
>> saved permanently as a file, point the HTML src attribute to a PHP scrip=
>> t, =
>>
>> possibly with some GET variables, and let that script output an image, =
>>
>> using the GD or ImageMagick libraries for instance.
>> -- =
>>
>> Rik Wasmus
>
> No Rik thats not correct - my fault - The image is not dynamically
> created its a file on the disk that is delete after use. It was the text
> file I created.
>
> Consider - these are the cases:
> If the link is not relative to the CWD the image is sent fine and deleted
> afterwards. Everything works as I want it to. (Except I need the images
> elsewhere)
>
> If the image is at a relative location to CWD and the image is deleted
> after it should have already been sent (or GET-ed if you like) - it is
> never seen.
>
That is correct operation. The img tag is a directive to the browser to
load the image. However, right after creating the image, you unlink the
file. So when the browser requests the file, it no longer exists.
> If it is at that relative location - and NOT deleted it is sent fine.
>
Correct operation.
> The only thing that has changed between working and not working is the
> image location from . to ../ or any other relative location.
>
You also have to figure caching in here. It may or may not be involved.
But don't delete a file before it's being used.
Probably the best would be to have the img point to a PHP file which
serves the image then deletes it.
> This is why I'm still confused.
>
> thanks
> -jools-
>
>
>
>
>
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Massive problem with output buffering
am 23.11.2007 00:22:22 von jools
In article ,
jstucklex@attglobal.net says...
>
> Probably the best would be to have the img point to a PHP file which
> serves the image then deletes it.
>
>
Yes thanks Jerry - I'm still confused by the behaviour but thats
what I've done in the end. Its somewhat disturbing to struggle like
this with a sequence problem!
thanks
-jools-