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;
}