fcicqbbs
注册会员

UID 74804
精华
0
积分 96
帖子 110
金钱 96 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2006-6-10
状态 离线
|
[广告]: 代充Paypal帐号美元
喜悦原创首发-由文件下载引起的
上次得到了这小小的一点奖励,继续写,支持一下.
本文本来于7.16就定稿,但可惜的是赶了个晚集,呵呵....
=====
初级版:
php提供了内置的函数,可以使用readfile() 来实现文件的直接输出.
readfile('1.mp3');
提高版:
你可以打开allow_url_fopen来输出外部的文件.
readfile('http://xxx/1.mp3');
但是如果你想用FTP的话,就需要这样做:
readfile(ftp_get($conn_id, $local_file, $server_file, FTP_BINARY));
中级版:
手册中有这么一句:
Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Reference CL, Stream Functions.
并且,偶想实现盗链.所以有了使用stream的代码
$context=array('http' => array ('header'=> 'Referer: http://xxxxxxx/'));
$xcontext = stream_context_create($context);
readfile('http://localhost/1.mp3',0,$xcontext);
这些代码运行的也不错.
高级版:
下面要做的就是断点续传的php实现.具体你怎么取得range,是你自己的事情.
$context=array('http' => array ('header'=> 'Range: bytes=1024-'));
$xcontext = stream_context_create($context);
readfile('http://localhost/1.mp3',0,$xcontext);
注意!不对了!
在调试了许多次后,发现php并不支持除200外的任何返回值.
偶向php提交了偶的bug报告.
http://bugs.php.net/bug.php?id=36857
欣慰的是,1天后得到了这样的消息,就算偶给php做了点小小的贡献...
This bug has been fixed in CVS.
(另:最喜欢看这句: Thank you for the report, and for helping us make PHP better.)
- Fixed bug #36857 (Added support for partial content fetching to the HTTP streams wrapper). (Ilia)
(Bug #36857 was fixed,bringing support for partial content fetching to the HTTP streams wrapper across all current PHP branches [Ilia])
参见:#36857
这样,你现在可以使用这段代码了(版本要求 4.X: >4.4.3RC1 5.X:>5.1.3)
终极版A:
$file = fsockopen("www.xxxx.com", 80, $errno, $errstr, 12);
fputs($file, "GET /xxxxxx HTTP/1.0\r\n");
fputs($file, "Host: www.xxxx.com\r\n");
fputs($file, "Referer: http://xxxxx/\r\n");
fputs($file, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n\r\n");
做完这些工作,你可以把这文件当普通文件用了.够简单吧...
终极版B:cURL
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
这东西效率比纯粹的方法高不少.
由于它不是核心自带的东西,所以不做过多讨论
感谢大家跟偶绕了一大圈,解决了一个很简单的文件下载问题.
联系作者fcicq: fcicqbbs at gmail dot com.
如果有问题,或者错误,均可联系。呵呵。
版权声明:原创
最后一句:喜悦首发,严禁转帖
fcicq 2006.7.27
|  doc |
|