php 下载远程文件到本地

 /**
     * Desc: 下载远程文件到本地
     * Date:2022/3/19 10:28
     * @param $url
     * @return string
     */
    function download_as_local_file($url, $pic_local_path = '')
    {
        $file = @file_get_contents($url);
        if (!$file) return '';
        $pathArr    = explode('/', $url);
        $fileName   = array_pop($pathArr);
        $time       = time();
        $randomStr  = strtolower(get_rand_char(6));
        $pathInfo = pathinfo($url);
        if (!isset($pathInfo['extension'])) return '';
        $fileName = $randomStr.'_'.md5($time.'_'.$fileName.$randomStr).'.'.$pathInfo['extension'];
        $pic_local_path = $pic_local_path ?: '/uploads/lazada/kd/video_temp/'.date('Ymd').DS;
        $pic_local = ROOT_PATH . 'public' .$pic_local_path .$fileName;

        if (!file_exists($pic_local)) {
            @mkdir(ROOT_PATH . 'public' .$pic_local_path, 0777, true);
            @chmod(ROOT_PATH . 'public' .$pic_local_path, 0777);
        }

        if (file_put_contents($pic_local, $file)) {
            return $pic_local_path.$fileName;
        }

        return  '';
    }

你可能感兴趣的:(PHP功能,php)