/**
* @param $url 下載下傳位址
* @param string $publicDir 檔案存放位址
* @param string $filename 檔案名稱
* @param int $type
* @return array|bool
*/
public function getDownload($url, $publicDir = '', $fileName = '', $type = 0) {
//擷取檔案路徑
$newOneDir = substr($publicDir,0,strrpos($publicDir,"/"));
if (trim($url) == '') {
return false;
}
//檢驗通路url是否有效
$array = get_headers($url,1);
if(!preg_match('/200/',$array[0])){
return false;
}
if (trim($publicDir) == '') {
return false;
}
//建立儲存目錄
if (!file_exists($newOneDir) && !mkdir($newOneDir, 0777, true)) {
return false;
}
//原來檔案存在則删除
if (file_exists($save_dir)) {
unlink($save_dir);
}
//擷取遠端檔案所采用的方法
if ($type) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
} else {
ob_start();
readfile($url);
$content = ob_get_contents();
ob_end_clean();
}
$size = strlen($content);
//檔案大小
$fp2 = @fopen($publicDir, 'a');
fwrite($fp2, $content);
fclose($fp2);
unset($content, $url);
return array(
'file_name' => $fileName,
'save_path' => $publicDir,
'file_size' => $size
);
}