By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,462 Members | 3,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,462 IT Pros & Developers. It's quick & easy.

Massive problem with output buffering

P: n/a


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

-----------------------------

Nov 21 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
On Wed, 21 Nov 2007 21:57:27 +0100, <jo***@by.comwrote:
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
Nov 21 '07 #2

P: n/a
In article <op***************@metallium.lan>, lu************@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-
Nov 21 '07 #3

P: n/a
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-
Nov 21 '07 #4

P: n/a

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-

Nov 21 '07 #5

P: n/a
On Wed, 21 Nov 2007 22:15:10 +0100, <jo***@by.comwrote:
>
>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
<http://nl2.php.net/manual/en/ref.outcontrol.php>. What different buffers
are active at any point in the code can be checked with
ob_get_status(true);
--
Rik Wasmus
Nov 21 '07 #6

P: n/a
jo***@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?
>
Nov 21 '07 #7

P: n/a
jo***@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);
Nov 21 '07 #8

P: n/a
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 (<img>) 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("<img src=$mypath alt='File missing'>");
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("<img src=$mypath alt='File missing'>");
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-


Nov 21 '07 #9

P: n/a
In article <11****************@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-
Nov 21 '07 #10

P: n/a
On Thu, 22 Nov 2007 00:50:46 +0100, <jo***@by.comwrote:
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 (<img>) 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("<img src=$mypath alt='File missing'>");
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 file.
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 script,
possibly with some GET variables, and let that script output an image,
using the GD or ImageMagick libraries for instance.
--
Rik Wasmus
Nov 22 '07 #11

P: n/a
In article <op***************@metallium.lan>, lu************@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-

Nov 22 '07 #12

P: n/a
On Thu, 22 Nov 2007 01:19:05 +0100, <jo***@by.comwrote:
In article <op***************@metallium.lan>, lu************@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.
-- =

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 come?
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 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.
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
Nov 22 '07 #13

P: n/a
jo***@by.com wrote:
In article <11****************@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-
Nov 22 '07 #14

P: n/a
jo***@by.com wrote:
In article <op***************@metallium.lan>, lu************@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.
js*******@attglobal.net
==================

Nov 22 '07 #15

P: n/a
In article <H-******************************@comcast.com>,
js*******@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-
Nov 22 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.