How to speed up file_get_contents?

前端 未结 3 1271
暗喜
暗喜 2021-02-07 19:44

Here\'s my code:

$language = $_GET[\'soundtype\'];
$word = $_GET[\'sound\'];
$word = urlencode($word);
if ($language == \'english\') {
    $url = \"

        
3条回答
  •  迷失自我
    2021-02-07 19:56

    Instead of downloading the whole file before outputting it, consider streaming it out like this:

    $in = fopen($url, 'rb', false, $context);
    $out = fopen('php://output', 'wb');
    
    header('Content-Type: video/mpeg');
    stream_copy_to_stream($in, $out);
    

    If you're daring, you could even try (but that's definitely experimental):

    header('Content-Type: video/mpeg');
    copy($url, 'php://output');
    

    Another option is using internal redirects and making your web server proxy the request for you. That would free up PHP to do something else. See also my post regarding X-Sendfile and friends.

提交回复
热议问题