Stream set and get

Hello!

I want to understand more about streams (I know what are IPs, sockets and ports but, never got into deep stuff) so I took a look over almost all stream_… functions and started to read some stuff (I got [URL=“http://christophh.net/2012/07/24/php-socket-programming/”]a great article, for those who’d like to know more).

Because I want to find out things all by myself, I started a small project - upload a file in the browser and check the loaded size in a window terminal (this window terminal can also be an Ajax request).
However I want to complete this task but I bumped into a stupid thing. I’ll post my code first.

upload.php


<?php

error_reporting(E_ALL);
set_time_limit(0);

if( isset($_POST['upload']) ) {

	$server = stream_socket_server("tcp://127.0.0.1:7891", $errno, $errorMessage);
	if( false == $server ) {
		die("Fail: $errorMessage");
	}
	$conn = stream_socket_accept($server);
	if( false == $conn ) {
		die('Connection not accepted!');
	}
	stream_set_write_buffer($conn, 0); // not sure how this will affect me
	/* here's the issue */ $contents = fwrite($conn, 'Loaded '.filesize($_FILES['file']['tmp_name']).' FROM '.$_FILES['file']['size']);
	move_uploaded_file($_FILES['file']['tmp_name'], 'file.tmp');
	fclose($server);

}

?>

<form action="" method="post" enctype="multipart/form-data">
	<input type="file" name="file" /> <input type="submit" name="upload" value="Upload" />
</form>


Of course, this thing doesn’t work, because the server will send the message when the file is 100% uploaded.
Now, my big question: Is there a way to get the filesize “on the fly”, while it writes on the disk?

my check_server.php script:


<?php

for(;;) {

	$client = @stream_socket_client("tcp://127.0.0.1:7891", $errno, $errorMessage);
	if ($client === false) {
		echo "No connection.\
";
	} else {
		
		while ( !feof($client) ) {
			echo fgets($client, 1024);
		}
		fclose($client);
	}
	
	sleep(1);

}


If you’re using PHP 5.4 you can use the new Session Upload Progress feature which allows you to retrieve information about files been uploaded in real time using an Ajax request, however if you’re on PHP 5.3 <= then you can use [URL=“http://pecl.php.net/package/uploadprogress”]uploadprogress extension for PECL.

That’s not the issue. I need to understand more about sending files with streams and how can I connect the upload to the stream.
As a progress bar example, I know about http://blueimp.github.io/jQuery-File-Upload/ and it’s quite a great tool.