The permission of fopen 644 cannot open the stream file.

the requirement is to download the remote file locally. Here is the download function. Fopen executes smoothly when I download pictures or text types. When the downloaded resource is video or audio, you will be prompted failed to open stream: Permission denied . You need to set the file permission to 755. How to solve this problem?

function download_remote_file($url , $ext,  $path = "/public/download/")
        {
            $ch = curl_init();
            curl_setopt($ch , CURLOPT_URL , $url);
            curl_setopt($ch , CURLOPT_RETURNTRANSFER , 1);
            curl_setopt($ch , CURLOPT_CONNECTTIMEOUT , 30);
            $file = curl_exec($ch);
            curl_close($ch);

            $fileSource = pathinfo($url);

            if (empty($fileSource["extension"])) {
                $filename = $fileSource["filename"] . "." . $ext;
            } else {
                $filename = $fileSource["basename"];
            }

            $resource = fopen($path . $filename , "a");

            fwrite($resource , $file);
            fclose($resource);
            return $filename;
        }
Php
Mar.19,2021

then change the permission
sudo chmod-R 755 / public/download/

Menu