大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区

标题: php模拟手机端下载文件 [打印本页]

作者: 追影    时间: 2024-1-2 10:59
标题: php模拟手机端下载文件
  1. public function download($url)
  2.     {
  3.         try {
  4.             if(startsWith($url,'//')) $url = 'https:'.$url;
  5.             $file_path = DIR_ROOT . 'image/files/';
  6.             $key = md5(time().mt_rand(1000,9999)).'.pdf';
  7.             $full_path = $file_path . $key;
  8.             if(!is_file($full_path)){
  9.                 $ch = curl_init();
  10.                 curl_setopt($ch, CURLOPT_URL, $url);
  11.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12.                 $h = array('User-Agent:Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
  13.                     'HTTP_ACCEPT:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
  14.                 curl_setopt($ch, CURLOPT_HTTPHEADER, $h);
  15.                 $rawdata = curl_exec($ch);
  16.                 curl_close($ch);
  17.                 $fp = fopen($full_path, 'w');
  18.                 fwrite($fp, $rawdata);
  19.                 fclose($fp);
  20.                 $this->uploadToBudget($full_path,$key);
  21.                 return $key;
  22.             }
  23.             return false;
  24.         } catch (\Exception $exception) {
  25.             echo $exception->getMessage();
  26.             return  false;
  27.         }
  28.     }
复制代码







欢迎光临 大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区 (https://www.damicms.com/bbs/) Powered by Discuz! X3.1