Non blocking functions PHP

后端 未结 7 1773
粉色の甜心
粉色の甜心 2020-12-29 16:12

I have a project in which a user uploads an image through a form and the server does some thumbnails. The thumbnail making process is very slow so I thought that doing the i

7条回答
  •  [愿得一人]
    2020-12-29 16:51

    You can use http headers to tell the client that the output has ended after the "OK" message has been transfered, while keeping the script running on the server to process the thumbnails. I've once successfully used this code:

    header("Connection: close");
    @ob_end_clean();
    ignore_user_abort();
    ob_start();
    
    //generate and print server response here
    echo "everything OK";
    
    $size = ob_get_length();
    header("Content-Length: ".$size);
    ob_end_flush();
    flush();
    
    //whatever you do here has no influence on the page loading time, as the client has already closed its connection.
    generateThumbnail();
    

提交回复
热议问题