SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)

    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 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 Code:
    <?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($conn0); // 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 Code:
    <?php

    for(;;) {

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

    }

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 uploadprogress extension for PECL.

  3. #3
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •