SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member JasonHenry's Avatar
    Join Date
    Feb 2010
    Location
    Minnesota, USA
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need a way to give updates while script processes

    Hi. I am working on a backend for a simple flash arcade site as a php learning experience. I have written a script and am trying to figure out a way to give updates while the script runs.

    1)It downloads a JSON feed which can reach 30 Mb and saves it locally
    2)Parses the data from the local file and populates it to a db table

    I have tried various things with flushing the output buffer, but I don't think I have a good enough grasp on the concept. Just looking for any suggestions as to the methods to accomplish this. The file downloading is via curl. I'm still fuzzy how javascript affects php as the server parses the whole script first as I understand.

    Thanks!

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    flush() will send data to the browser, but I believe there can be issues with the server's buffer, and on top of that, there are issues with the browser's buffer. In FF, flush() is enough, I've tested the following in FF and Chrome, both work on my setup.
    PHP Code:
    echo str_repeat(" "1024); // needed for chrome, I think IE is 256
    flush();
    for(
    $x=0$x<5$x++)
    {
        
    sleep(1);
        echo 
    'working ...<br>'// <br> needed for chrome, \n might be needed for some browsers
        
    flush();


  3. #3
    SitePoint Member JasonHenry's Avatar
    Join Date
    Feb 2010
    Location
    Minnesota, USA
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input hash. I tried your code on my apache server/windows machine and server host's linux box and it didn't flush the output until the script was finished running. (FF) I put this code in a section of code which downloads a SWF and PNG image from a server and writes to local files. The text did not appear until the actions were finished...then output the 5 "working..." strings. I do not have a good working knowledge yet of the these deeper php functions and methods. Seen a lot of talk about the flush() function in various how-to's but I guess I just haven't seen any magic yet.

    What is the purpose of echoing the spaces? Is it for formatting or some affect on the buffer?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird that it didn't work in FF, I can only assume this is because of the server config. All I need is the for loop for FF (on both win and linux).

    The spaces are to fill up the browsers buffer. Once full it will output to the screen. Took 1024 bytes for Chrome.

  5. #5
    SitePoint Member JasonHenry's Avatar
    Join Date
    Feb 2010
    Location
    Minnesota, USA
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could the file operations make a difference?

    Here is a function I put your code into. Perhaps I am not doing it correctly.

    PHP Code:
    function install_mochigame($gameid) {
        
    $result yas_query("SELECT * FROM `mochigames` WHERE `id` = '$gameid'");
        
    $gamename $result[1];
        
    $thumburl $result[7];
        
    $gameurl $result[2];
        echo 
    str_repeat(" "1024); // needed for chrome, I think IE is 256
        
    flush();
        for(
    $x=0$x<5$x++)
        {
            
    sleep(1);
            echo 
    'working ...<br>'// <br> needed for chrome, \n might be needed for some browsers
            
    flush();
        } 
        
        
    set_time_limit(0);       
        
    $fileurl str_replace(".."""$thumburl);    // Download thumbnail
        
    $thumb get_content_of_url($fileurl);    // uses cURL functions to get file
            
        
    $fileurl str_replace(".."""$gameurl);
        
    $swf get_content_of_url($fileurl);            // Download game file  
            
        
    $fconfig fopen("../img/" $gamename ".png"'wb');  // Save thumbnail
        
    fwrite($fconfig$thumb);
        
    fclose($fconfig);
        empty(
    $thumb);
        
        
    $fconfig fopen("../swf/" $gamename ".swf"'wb');    // Save game file
        
    fwrite($fconfig$swf);
        
    fclose($fconfig);
        empty(
    $swf);
                
        
    $gamefile 'swf/' $gamename '.swf';
        
    $gamethumb 'img/' $gamename '.png';  
        
    $category explode(","$result[8]);       // Remove comma delimiter and seperate categories into array
                                                    
        
    $category convert_category($category[0]);     
        
    $desc clean($result[5]);     //clean() sanitizes data for query
        
    $instructions clean($result['instructions']); 
        
    $keywords clean($result[6]);                              
            
    $gamename clean($gamename);
        
    $gamefile clean($gamefile);
        
    $gamethumb clean($gamethumb);
        
    $height $result[4];
        
    $width $result[3];
        
        
    $query mysql_query("INSERT INTO `games` (`id`, `title`, `description`, `instructions`, `keywords`, `file`, `height`, `width`, `category`, `plays`, `code`, `type`, `thumbnail`, `ismochi`) VALUES (NULL, '$gamename', '$desc', '$instructions', '$keywords', '$gamefile', '$height', '$width', '$category', 0, '', 'SWF', '$gamethumb', 1)");
        if (!
    $query) { 
            echo 
    'Error updating Games database';
            return 
    false;
        }
        
    $query mysql_query("UPDATE mochigames SET isinstalled = 1 WHERE id = '{$result[0]}'");
        if (!
    query) {
            echo 
    'Error updating mochigame database';
            return 
    false;
        }
        return 
    true;                                                     

    Code PHP:
     


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
  •