Page 1 of 2

Code blocking output

Posted: Fri Jun 09, 2006 5:40 am
by espadav8
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all,

I'm fairly new to PHP and I've been trying to get an FTP upload feacture working. So far I've got the code that will upload it, but on the page I want some feedback as it doess things, however, I don't get anything back until the while page has finished. I was wondering if there was a way of doing this.

Code: Select all

echo "Attempting to connect to $mysql_result[url]<br/>";
		
		// open a ftp connection
		$connection_id = ftp_connect($mysql_result['url'], $mysql_result['port']);
		if (!$connection_id)
		{
			echo "Can't connect to FTP server<br/><br/>";
		}
		else
		{
			echo "Connected, now logging in<br/>";
			
			// login to the server
			$login_response = ftp_login($connection_id, $mysql_result['user'], $mysql_result['pass']);
			if (!$login_response)
			{
				echo "Can't login to FTP server<br/><br/>";
			}
I have bits of code like that. I've tried using buffered output and flushing after each 'echo' but that didn't make a difference.

Any help or pointers are welcomed :D

Thanks


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jun 09, 2006 5:53 am
by hairyjim
I think others have implemented PHP/Ajax progress meters. I have not tried any myself. I did bookmark one or two.

*Quick search in favourites*

http://www.devpro.it/upload_progress/

http://www.modphp.org/viewtopic.php?t=324

These may provide you with some info.

Posted: Fri Jun 09, 2006 8:57 am
by espadav8
Thanks for those links, however, i think it's a bit past my level yet :-( all i really wanted at this time was a line-by-line account of where the code was up to as it was happening, so i could see if something was taking longer than it should (e.g., if the FTP connection was taking a long time, so it would basiclly be like you get with a real ftp client as it tells you it's progress on connecting to the server, only not as much information).

Thanks

[EDIT]

at the moment when i submit the form that leads to the page it loads it and then after about 1 1/2 mins i get all the output below shown on the screen.

Code: Select all

Attempting to connect to 213.171.xxx.yyy
Connected, now logging in
Logged in. Passive mode
Changing dir to /htdocs/ftp-test/
Getting template
Attempting to upload file
File has been uploaded

Attempting to connect to www.example.com
Can't connect to FTP server
I hope this helps clear what I meant.

Thanks again
[/EDIT]

Posted: Fri Jun 09, 2006 9:39 am
by nathanr
the php manual is a great thing... php output control / buffering

Code: Select all

ob_start();
echo "Attempting to connect to $mysql_result[url]<br/>"; 
ob_end_flush();                
                // open a ftp connection 
                $connection_id = ftp_connect($mysql_result['url'], $mysql_result['port']); 
                if (!$connection_id) 
                {
                        ob_start();
                        echo "Can't connect to FTP server<br/><br/>"; 
                        ob_end_flush();
                } 
                else 
                { 
                        ob_start();
                        echo "Connected, now logging in<br/>"; 
                        ob_end_flush();                        
                        // login to the server 
                        $login_response = ftp_login($connection_id, $mysql_result['user'], $mysql_result['pass']); 
                        if (!$login_response) 
                        { 
                                ob_start();
                                echo "Can't login to FTP server<br/><br/>"; 
                                ob_end_flush();
                        }

Posted: Fri Jun 09, 2006 9:50 am
by hairyjim
espadav8 wrote:all i really wanted at this time was a line-by-line account of where the code was up to as it was happening
Ahhh sorry I thought you meant you wanted a file upload meter. My mistake.

Anyway nathanr's post above gives you your solution.

Posted: Fri Jun 09, 2006 10:17 am
by espadav8
nathanr wrote:the php manual is a great thing... php output control / buffering

Code: Select all

ob_start();
echo "Attempting to connect to $mysql_result[url]<br/>"; 
ob_end_flush();                
                // open a ftp connection 
                $connection_id = ftp_connect($mysql_result['url'], $mysql_result['port']); 
                if (!$connection_id) 
                {
                        ob_start();
                        echo "Can't connect to FTP server<br/><br/>"; 
                        ob_end_flush();
                } 
                else 
                { 
                        ob_start();
                        echo "Connected, now logging in<br/>"; 
                        ob_end_flush();                        
                        // login to the server 
                        $login_response = ftp_login($connection_id, $mysql_result['user'], $mysql_result['pass']); 
                        if (!$login_response) 
                        { 
                                ob_start();
                                echo "Can't login to FTP server<br/><br/>"; 
                                ob_end_flush();
                        }
Hi nathanr,

I tried that before (I found someone mention it in 'thing that make you go WOAH' or something like that :)), only I used ob_flush() instead. I've just tried it again with ob_end_flush() and it hasn't made any difference :(

I'm not completely sure but could it be because something is blocking the flow? I've just tried changing ftp_fput to ftp_nb_fput but that also doesn't seem to have made a difference. I'm going to try and put the files onto a live site since I'm currently running everything through a VM so I can show it.

Thanks again :)

Posted: Fri Jun 09, 2006 10:21 am
by nathanr
output_buffering boolean/integer
You can enable output buffering for all files by setting this directive to 'On'. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096). As of PHP 4.3.5, this directive is always Off in PHP-CLI.
might have something to do with it, can be changed in php.ini

Posted: Fri Jun 09, 2006 10:48 am
by espadav8
http://www.espadav8.co.uk/ftp/index.php

I've very quickly uploaded it there. If you select test 2 or 3 and submit and then select the top ftp site and submit that form you'll see what happens when it can connect to the ftp site and upload the file (if you really want to see it it uploads it to http://www.espadav8.co.uk/ftp-test/andy.htm). As you can hopefully tell it renders very quickly so one line at a time doesn't really make much sense, however, If you then go back and select both of the ftp sites and submit again the page will just load for about 1 1/2 mins and then display everything. What i'd like it to do is display each line so you can see where it's hanging (in this case it'd be when it's trying to connected to http://www.example.com, but it could happen elsewhere).

I'll check the php.ini file as well, thanks :)

[EDIT]

I've just checked my php.ini and this iss what grep spat out, so it looks like it's already on.

Code: Select all

[asmith@localhost ~]$ cat /etc/php.ini | grep output_buffering
; - output_buffering = 4096        [Performance]
; a value for this directive (e.g., output_buffering=4096).
output_buffering = 4096
;       performance, enable output_buffering in addition.
[/EDIT]

Posted: Fri Jun 09, 2006 11:02 am
by RobertGonzalez
When you say that the code suddenyl spits out everything after a minute and a half, that kinda indicates that there is something going on for the minute and half. Then suddenly it connects and processes what it is supposed to. I don't think there is a problem with your script, I think there is a problem with the ftp connection. Another thing to try is maybe set a disconnection at the end of the script to free up the connection resource. That may be what is causing the excessive hang that you are seeing. But what should happen is an apparent and almost immediate display of data to the screen when using echo.

Posted: Fri Jun 09, 2006 11:20 am
by espadav8
The code is trying to do something. A basic run down of it is...

1) get a list of FTP sites selected from the DB
2) select the next one
3) try and connect to it (this is the reason it hangs for 90secs with example.com because it doesn't exist)
4) login
5) cd into a folder
6) upload a file
7) disconnect
8) goto step 2 until there are no left

As it currently stands, i know the first site exists (it's my site) and i know the second one doesn't (it's example.com). The first site doesn't have any problems, when you try and publish to example.com then it hangs until the connection attempt fails and then it goes onto the next one (or ends in this case with 2 sites).

The reason I need this is because when there are, say, 20 sites, if one of them doesn't respond then it would be good to know as it's going through the list rather than having to wait for it to timeout. If there were 2 to fail then you wouldn't know until after 3mins, and so on.

I thought echo should output to the screen without a delay, and at the end of the repeating loop i close the ftp connection that was created so it can be recreated again.

Thanks

Posted: Fri Jun 09, 2006 11:43 am
by RobertGonzalez
The challenge with what you are doing is that while pconnect tries to establish the connection, there is not way for you to output anything to the screen using PHP. This is something that might better be served with an AJAX front end. Using PHP to echo out a status during the call to ftp_pconnect is not possible as far as I know.

Posted: Fri Jun 09, 2006 12:27 pm
by nathanr
espadav8 wrote:http://www.espadav8.co.uk/ftp/index.php

I've very quickly uploaded it there. If you select test 2 or 3 and submit and then select the top ftp site and submit that form you'll see what happens when it can connect to the ftp site and upload the file (if you really want to see it it uploads it to http://www.espadav8.co.uk/ftp-test/andy.htm). As you can hopefully tell it renders very quickly so one line at a time doesn't really make much sense, however, If you then go back and select both of the ftp sites and submit again the page will just load for about 1 1/2 mins and then display everything. What i'd like it to do is display each line so you can see where it's hanging (in this case it'd be when it's trying to connected to http://www.example.com, but it could happen elsewhere).

I'll check the php.ini file as well, thanks :)

[EDIT]

I've just checked my php.ini and this iss what grep spat out, so it looks like it's already on.

Code: Select all

[asmith@localhost ~]$ cat /etc/php.ini | grep output_buffering
; - output_buffering = 4096        [Performance]
; a value for this directive (e.g., output_buffering=4096).
output_buffering = 4096
;       performance, enable output_buffering in addition.
[/EDIT]
you'll be wanting to padd the strings out to 4096 so they get buffered then..

Code: Select all

echo str_pad("string of text here", 4096);

Posted: Mon Jun 12, 2006 10:14 am
by espadav8
Hi, Sorry about the delay in replying, but I fell ill over the weekend (still am). I'll try padding out the string, and if that fails then... well, for now I'll just not bother but then I'll try and do some AJAX thing when I know more about PHP.

Thanks for the help everyone :-)

Posted: Mon Jun 12, 2006 10:23 am
by RobertGonzalez
nathanr wrote:you'll be wanting to padd the strings out to 4096 so they get buffered then..

Code: Select all

echo str_pad("string of text here", 4096);
This still won't work to do what he wants. The OP wants to be able to output messages during the call to pconnect(). During that time span there is nothing being sent to the browser, there is just an attempt being made to connect. Once there is/isn't a connection made, PHP returns a value of some sort. But during the attempt to connect there is nothing be sent back to the browser. That is why I suggested something client side.

Posted: Mon Jun 12, 2006 12:14 pm
by nathanr
I can see what you mean there, perhaps some simply echo'ing out the responses from the ftp server? not sure if they are sent back or not without reading further into it.. as far as client side goes.. surely if you used ajax, it would still be the same php script being pulled by ajax.. surely then the sript would still send nothing to the browser (or ajax) until completion. :?:

would using a socket not work?