How does PHP transfer large files through http?

< H2 > "Front end" is UE4, downloading large files from my nginx server through http protocol. File transfer must be encrypted using base64. Baidu has many transmission methods on the Internet. These methods are all aimed at the case that the front end is a browser. But my current situation is that my server only provides a data interface, and "front end" is not a browser. Baidu"s method is as follows: < / H2 >

1.fread segmented read
2.readfile entire read
3.file_get_counts entire read
4.curl transfer

< H2 > he is encrypted through base64, so it seems that segmented transmission cannot be realized, so the whole file can only be read out, encrypted by base64, and then transferred. My code is as follows: < / H2 >
public function actionPak()
    {
        set_time_limit(0);
        //
        $post_data = file_get_contents("php://input");
  
       // json
        $json_obj = json_decode($post_data);
        if (isset($json_obj->Num)) {
            $file = \Yii::getAlias("@webroot") . "/pattern/pak/" . $json_obj->Num . ".pak";
            //
            ob_start();
            readfile($file);
            $data = ob_get_contents();
            ob_end_clean();
            return base64_encode($data);
        } else {
            return base64_encode("false");
        }
    }

all methods have been tried except for curl. The problem now is that there is no problem with small file transfer. When the file size is tens to hundreds of megabytes, the transfer will fail. Do you have any advice from the boss? thank you

Php
Jun.26,2021

both the server and PHP have a maximum timeout setting. Have you made it infinite?


  1. check the configuration that nginx needs to transfer large files.
  2. who says base64 can't be transmitted in segments? Base64 is a string, and segmentation is too convenient.

to read in segments, base64 encoding, upload, server base64 decoding, append mode writing. When uploading, just upload it in order. If you don't follow the order, add a number each time, and the server will write to the corresponding segment, but the processing on the server side is a bit cumbersome.

Don't set too large a request length, without limits.

Menu