<?php
//計算遠程檔案大小
function remote_filesize($url,$user='',$pw='')
{
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
if (!empty($user)&&!empty($pw)) {
$headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$okay = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
if (isset($matches[1])) {
$size = $matches[1];
} else {
$size = 'unknown';
}
return $size;
}
function get_file($url, $folder = "./") {
set_time_limit (24 * 60 * 60); // 設定逾時時間
$destination_folder = $folder . 'faguicn/'; // 檔案下載儲存目錄,預設為當前檔案目錄
if (!is_dir($destination_folder)) { // 判斷目錄是否存在
mkdirs($destination_folder); // 如果沒有就建立目錄
}
$newfname = $destination_folder . basename($url); // 取得檔案的名稱
$file = fopen ($url, "rb"); // 遠程下載檔案,二進位模式
if ($file) { // 如果下載成功
$newf = fopen ($newfname, "wb"); // 遠在檔案檔案
if ($newf) // 如果檔案儲存成功
while (!feof($file)) { // 判斷附件寫入是否完整
fwrite($newf, fread($file, 1024 * 8), 1024 * 8); // 沒有寫完就繼續
}
}
if ($file) {
fclose($file); // 關閉遠程檔案
}
if ($newf) {
fclose($newf); // 關閉本地檔案
}
return true;
}
function mkdirs($path , $mode = "0755") {
if (!is_dir($path)) { // 判斷目錄是否存在
mkdirs(dirname($path), $mode); // 迴圈建立目錄
mkdir($path, $mode); // 建立目錄
}
return true;
}
// 使用樣本
//串連資料庫
$user="root";
$host="localhost";
$password="root";
$dbname="test"; //所查詢的庫表名;
//串連MySQL資料庫
mysql_connect("$host","$user","$password");
$db = mysql_select_db("$dbname") or die("無法串連資料庫!");
$sql = "SELECT * from table WHERE 1";//產生查詢記錄數的SQL語句
$rst = mysql_query($sql) or die("無法執行SQL語句:$sql !"); //查詢記錄數
while($row=mysql_fetch_array($rst)){
if($row['cn_url']!=""){
$url = "http://file.foodvip.net/".$row['cn_url'];
//echo $url."<br/>";
//檢查遠程檔案是否存在。
/*
$file_up=$row['cn_url'];
if(file_exists($file_up)){
//echo "<font color='red'>檔案已經存在!!",'</font><br />';
}else{
echo $row['title'].' '.$row['cn_url']."該檔案不存在!!",'<br />';
}*/
if(get_file($url)){
echo '1','<br/>';
}else{
echo '失敗',$row['title'];
}
/*$i2 = "CCFFV/".$row['bzname'].".pdf";
$i1= $row['cn_url'];
$ii =rename($i1,$i2);
if($ii){
echo "修改成功";
}else{
echo "修改失敗";
}*/
}
//echo get_file("http://huishi.foodmate.net/biaofa/lawuploads/".$row['cn_url']."");
}
/*
//$url = "http://ceshi.com/2072027.pdf";
$filesize = remote_filesize($url,$user='',$pw='');
//顯示遠程檔案大小
$ii = substr($filesize,0,5)."kB";
if($ii=="169kB"){
echo get_file('http://ceshi.com/29084546.pdf');
}
*/
//判斷檔案是否存在
?>