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);
}