473,569 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Massive problem with output buffering



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
15 1805
On Wed, 21 Nov 2007 21:57:27 +0100, <jo***@by.comwr ote:
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
In article <op************ ***@metallium.l an>, 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
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

Just so you dont all think I'm completely mad - if I just do this

// unlink('text.tx t');

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
On Wed, 21 Nov 2007 22:15:10 +0100, <jo***@by.comwr ote:
>
>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(t rue);
--
Rik Wasmus
Nov 21 '07 #6
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
jo***@by.com wrote:
Just so you dont all think I'm completely mad - if I just do this

// unlink('text.tx t');

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
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
In article <11************ ****@proxy00.ne ws.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4940
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or...
2
1708
by: Mark Evenson | last post by:
A question about Output Buffering in PHP-CLI. Is it expected that the following code will emit two representations of $x? I would like to use print_r(), but capature its output in a String, to use ain a generic logging infrastructure. But every call to print_r(), var_dump(), var_export(), et. al. goes to STDOUT no matter what I do? How...
2
1982
by: vishal | last post by:
hi vishal here. can anyone tell me that how to buffer output and then send the output to client at a time. what is my problem is that i have a php file which runs query on database and takes some time to get query result. so what i want is i want to buffer output till the query returns result and then send the buffered output to client...
1
4766
by: nushin | last post by:
If you do not null out the output of your parent process that launches a child process by os.spawnv( ) in P_NOWAIT mode, you will see that your child process is launched *synchronously* instead of asynchronously. This is a bug! Here's a code sample: In your parent.py file, call child.py as ...
4
1485
by: Hallvard B Furuseth | last post by:
I have a program which starts by reading a lot of data into various dicts. When I moved a function to create one such dict from near the beginning of the program to a later time, that function slowed down by a factor of 8-14: 38 sec at 15M memory usage, 570 sec at 144M, 330 sec at 200M. Is there anything I can do to fix that? When the...
9
2702
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've looked at the C++ standard (ISO/IEC 14882) from the web to get a grip on the relationship between ostream methods and streambuf methods: flush(),...
7
1819
by: Les Coover | last post by:
I have struggled and struggled with this and still can not get it. The endless loop is supposed to be that way so that strings can be entered over and over again. When finished CTRL + C returns to UNIX $ prompt. I know this is crude but I am just trying to get the basics. All I want to do is enter data into a text file, the following...
49
1695
by: iesvs | last post by:
I got a double ** (double **coef). And gcc accept the syntax coef, it works. I don't understand why. For me coef is not correct because it's a double ** and not type. If someone could explain me that.
4
1592
by: Grant Edwards | last post by:
When I ssh in to my Windows XP box and run Python apps, output from "print" and and "sys.stdout.write()" is being buffered so that none of the output shows up until the program exits. From within my program how do I set output buffering to either line-buffered or un-buffered? -- Grant Edwards grante Yow!...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.