Stream set and get


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=“”]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.




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

	$server = stream_socket_server("tcp://", $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');



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

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:


for(;;) {

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


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=“”]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 and it’s quite a great tool.