綠色資源網:您身邊最放心的安全下載站! 最(zuì)新軟件|熱門排行|軟件分類|軟件(jiàn)專(zhuān)題|廠商大全

綠色資源網

技術教程(chéng)
您的位置:首(shǒu)頁網絡編(biān)程PHP編程 → 用php實現遠程網絡文件下載(zǎi)到服務器

用php實現遠程網絡文件下載到服務器

我要評論 2009/09/14 11:55:39 來源:綠(lǜ)色資源網 編輯:Downcc資源站 [ ] 評(píng)論:0 點擊:520次

本文主要介(jiè)紹如何用php程序(xù)實現將遠程網絡文件下載到自己的主機(jī)(服務器、虛(xū)擬主機)上,當然也提供asp、asp.net的源碼給(gěi)大家參考.

最近讓 Lc. 下載《汪洋中的一條船》這部感人的勵誌電視劇,為了分享方便,於是我(wǒ)提供一個Godaddy的免(miǎn)費FTP.

考慮(lǜ)到穩定(dìng)性和專一性,我想重新開啟一個godaddy的(de)免費空間(jiān),但是此時已經(jīng)上傳了兩集,一共是400多兆.怎麽辦呢?

於是乎我想起了之前轉移空間時用到的一個php文件,他可以下載遠程網絡文(wén)件到(dào)服務器上,也就是隻要(yào)是可以下載的url,就可以轉移到新的服務器上.

廢話不多說,直接亮(liàng)出將遠程網絡文件下載到服(fú)務器的php源碼(測試(shì)通過,很好很強大).

 <form method="post">
<input name="url" size="50" />
<input name="submit" type="submit" />
</form>
< ?php
// maximum execution time in seconds
set_time_limit (24 * 60 * 60);
if (!isset($_POST['submit'])) die();
// folder to save downloaded files to. must end with slash
$destination_folder = 'temp/';
 
$url = $_POST['url'];
$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);
}
?>

當然還需要展示下asp版本的源碼(未測試)

< %
function   downfilea(d_target,   s_target)
On   Error   Resume   Next  
    dim   myhttp,   objstream
    set   myhttp   =   server.createobject( "MSXML2.XMLHTTP ")
    myhttp.open   "GET ",   d_target,   false
    myhttp.send()
    set   objstream   =   Server.CreateObject( "adodb.stream ")  
    objstream.Type   =   1  
    objstream.Mode   =   3  
    objstream.Open
    objstream.Write   myhttp.responseBody
    objstream.SaveToFile   s_target,   2
    if   err.number   <>   0   then   err.Clear
end   function
 
downfilea   "http://www.abc.com/xxx.rar ",   Server.MapPath( "down/xxx.rar ")
Response.write   "ok "
%>

還有asp.net版本(běn)的源碼(未測試)

   using   System;  
  using   System.Net;  
  using   System.IO;  
 
  class   DownloadFile;  
      {  
        static   void   Main(string[]   args)  
            {  
                //你的遠程文件  
                string   siteURL="http://www.abc.com/xxx.rar";  
                //下載到本地的路(lù)徑及文件名  
                string   fileName="c:\\xxx.rar";  
                //實例化一個WebClient  
                WebClient   client=new   WebClient();  
                //調用WebClient的DownloadFile方法  
                client.DownloadFile(siteURL,fileName);  
                }  
        }

注意事項:

1.相關的目錄可能需要建(jiàn)立,具體請看源碼.比如temp/、down/.

2.其中php版本的源碼是支持自定義url的,但asp、asp.net版本的並沒有提供自定義(yì).

3.在拷貝大文件時,國內估計支持欠佳.因為考慮到程序的超時時間,但在國外支持很好(hǎo).

比如Blinux將一個近300M的文件轉移到另一個服務器(qì)上耗(hào)費時(shí)間是28秒.這是國內無法想象的.

最後希望(wàng)大家反饋下使用效果.

關鍵詞(cí):php,服務器

閱(yuè)讀本文後您有什麽感想? 已有 人給出評價!

  • 1 歡迎喜歡
  • 2 白癡
  • 1 拜托
  • 1 哇
  • 1 加油
  • 1 鄙視
免费人欧美成又黄又爽的视频丨一本色道久久88综合日韩精品丨国产专区日韩精品欧美色丨午夜无遮挡男女啪啪视频丨国产欧美日韩综合精品一区二区丨亚洲精品无码不卡在线播HE丨亚洲精品国产精品国自产观看丨日韩国产高清av不卡