遠程檔案下載代碼_PHP教程

來源:互聯網
上載者:User
遠程檔案下載代碼這裡為各位提供一款遠程檔案下載代碼,我們可以把遠端檔案用php下載到本地指定的目錄,下面就是一款下載遠程伺服器檔案代碼類。

遠程檔案下載代碼
這裡為各位提供一款遠程檔案下載代碼,我們可以把遠端檔案用php教程下載到本地指定的目錄,下面就是一款下載遠程伺服器檔案代碼類。

class download
{
var $url;//遠程檔案地址

var $file_name = "hdwiki.zip";//下載來的檔案名稱

var $save_path = "./www.bKjia.c0m";//下載到本地的檔案路徑

var $localfile;//下載到本地檔案的路徑和名稱

var $warning;//警告資訊

var $redown=0;//是否重新下載


/*初始化*/
function seturl($url)
{
if(!empty($url))$this->url = $url;
}

function setfilename($file_name)
{
if(!empty($file_name))$this->file_name = $file_name;
}

function setsavepath($save_path)
{
if(!empty($save_path))$this->save_path = $save_path;
}

function setredown($redown)
{
if(!empty($redown))$this->redown = $redown;
}

function download($url, $redown = 0, $save_path = 0, $file_name = 0)
{
$this->seturl($url);
$this->setfilename($file_name);
$this->setsavepath($save_path);
$this->setredown($redown);
if(!file_exists($this->save_path))
{
$dir = explode("/",$this->save_path);
foreach($dir as $p)
mkdir($p);
}
}

/* 檢查url合法性函數 */
function checkurl(){
return preg_match("/^(http|ftp)(://)([a-za-z0-9-_]+[./]+[w-_/]+.*)+$/i", $this->url);
}

//下載檔案到本地

function downloadfile()
{
//檢測變數

$this->localfile = $this->save_path."/".$this->file_name;
if($this->url == "" || $this->localfile == ""){
$this->warning = "error: 變數設定錯誤.";
return $this->warning;
}

if (!$this->checkurl()){
$this->warning = "error: url ". $this->url ." 不合法.";
return $this->warning;
}

if (file_exists($this->localfile)){
if($this->redown)
{
unlink($this->localfile);
}
else
{
$this->warning = "warning: 升級檔案 ". $this->localfile ." 已經存在! 重新下載";
return $this->warning;
//exit("error: 本地檔案 ". $this->localfile ." 已經存在,請刪除或改名後重新運行本程式.");

}
}

//開啟遠程檔案

$fp = fopen($this->url, "rb");
if (!$fp){
$this->warning = "error: 開啟遠程檔案 ". $this->url ." 失敗.";
return $this->warning;
}

//開啟本地檔案

$sp = fopen($this->localfile, "wb");
if (!$sp){
$this->warning = "error: 開啟本地檔案 ". $this->localfile ." 失敗.";
return $this->warning;
}

//下載遠程檔案

//echo "正在下載遠程檔案,請等待";

while (!feof($fp)){
$tmpfile .= fread($fp, 1024);
//echo strlen($tmpfile);

}
//儲存檔案到本地

fwrite($sp, $tmpfile);
fclose($fp);
fclose($sp);

if($this->redown)
$this->warning = "success: 重新下載檔案 ". $this->file_name ." 成功";
else
$this->warning = "success: 下載檔案 ". $this->file_name ." 成功";

return $this->warning;
}
}

http://www.bkjia.com/PHPjc/631992.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631992.htmlTechArticle遠程檔案下載代碼這裡為各位提供一款遠程檔案下載代碼,我們可以把遠端檔案用php下載到本地指定的目錄,下面就是一款下載遠程服務...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.