程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> php實現遠程網絡文件下載到服務器

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

編輯:PHP基礎知識
 

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

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

考慮到穩定性和專一性,我想重新開啟一個godaddy的免費空間,但是此時已經上傳了兩集,一共是400多兆.怎麼辦呢?

於是乎我想起了之前轉移空間時用到的一個php文件,他可以下載遠程網絡文件到服務器上,也就是只要是可以下載的url,就可以轉移到新的服務器上.

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

<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版本的源碼(未測試)

using System;
using System.Net;
using System.IO;

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

注意事項:

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

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

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

比如Blinux將一個近300M的文件轉移到另一個服務器上耗費時間是28秒.這是國內無法想象的.

最後希望大家反饋下使用效果.
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved