php如何获取http://xxx.com/down.asp?ID=5828并重命名保存? http://xxx.com/down.asp?ID=5828这是一个下载链接,file_get_contents好像不起作用,应该用什么样的方式来获取文件;另一方面链接并没有包含文件名称,这个链接有可以是rar也有可以是jpg,怎么得到具本的信息?感谢抽空看我的问题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用fsockopen或curl_*分析头文件 一般都能得到文件信息跟文件的地址 你再用那个地址就能获取到文件了 $url = parse_url("http://www.webshu.net/Soft/ShowSoftDown.asp?UrlID=1&SoftID=15947"); $fp=fsockopen($url['host'],80,$errno,$errstr); if($fp){ $header = "GET ".$url['path']."?".$url['query']." HTTP/1.1\r\n"; $header .= "Host: ".$url['host']."\r\n"; $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1\r\n"; $header .= "Referer: http://".$url['host']."\r\n"; $header .= "Connection: Close\r\n\r\n"; fwrite($fp, $header); //$f = fopen($path . basename($file), "wb"); while (!feof($fp)) { $s = fgets($fp,128); echo $s; } //fclose($jpg); fclose($fp);根据提示用fsockopen,上面代码结果显示如下:HTTP/1.1 200 OK Connection: close Date: Wed, 12 Aug 2009 13:20:02 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET Content-Length: 286 Content-Type: text/html Expires: Tue, 11 Aug 2009 13:20:02 GMT Set-Cookie: ASPSESSIONIDSADRDRBR=ILJBCCEBKGPKPBOJPKMCDIDC; path=/ Cache-control: no-cache 我从哪里可以得到真实的文件信息? 从地址中可以看到获取到的文件里包含迅雷下载的地址为:thunder://QUFodHRwOi8vZG93bi53ZWJzaHUubmV0L0Rvd25sYW9kL1NDL2N5L2xoMDczMDIwLnJhclpa 用base64_decode解密后:AAhttp://down.webshu.net/Downlaod/SC/cy/lh073020.rarZZ用trim和rtrim去掉AA和ZZ后,得出:http://down.webshu.net/Downlaod/SC/cy/lh073020.rar 这就是下载地址。这就是思路,其中地址是正确的。这个从网址上和Socket头上看不到下载的信息,从内容就能看到。 <?PHP/** * 输入外部流处理类 * * @author Jerry.Li([email protected]) * @access public * @package CInStream *---------------------------------------------------------------------- 版本: 0.1- 文件名:CInStream.class.php- 编写日期:2009/08/06- 运行环境:php5以上- 修改记录:2009/08/06,JerryLi,程序创立---------------------------------------------------------------------*//** * 输入流处理类 * 主要功能是载入外部链接或数据,文本或文件流 * */class CInStream{ function __construct() { if (!function_exists(get_headers)) { echo '请确认您的版本为PHP5或以上版本,否则程['. __CLASS__.']序无法正常工作。'; exit(0); } } function __destruct() { } /** * 载入外部文件保存到指定位置 * 特殊处理:如果$sFilePath相同,文件大小相同,的文件存在目标目录,则默认为相同的文件,不写入这个文件。 * 即带缓存的处理方式。 * * @param string $sFilePath 文件物理路径(可以为本地文件或者远程http上的文件) * @param string $sToPath 目标文件的保存路径 * @return null / shring 返回保存完成的文件名(md5文件名) */ public function load2File($sFilePath, $sToPath) { /*1、对传入的文件名进行处理*/ //判断是否远程文件 $sReadType = (strtolower(substr($sFilePath, 0, 4)) == 'http')? 'http' : 'local'; $sExt = preg_replace("/.*\.([^.]+)$/","\\1", $sFilePath);//分离扩展名 $sToPath = str_replace('\\', '/', $sToPath); $sToPath = (substr($sToPath, -1, 1) != '/') ? $sToPath .'/' : $sToPath; /*2、检测目标文件是否存在*/ if ($sReadType == 'http')/*检验远程文件是否存在*/ { $aTmp = @get_headers($sFilePath, 1); if (!is_array($aTmp) || !strpos($aTmp[0], '200 OK')) { unset($aTmp); return null;//远程文件不存在 } else { $iFileSize = intval($aTmp['Content-Length']);//获取文件大小 $aTmp = explode('/', $aTmp['Content-Type']);//单独处理html方式的扩展名 $sExt = (trim($aTmp[1]) == 'html')? 'html' : $sExt;//取出文件类型 unset($aTmp); } } elseif ($sReadType == 'local')//检验本地文件是否存在 { if (file_exists($sFilePath)) $iFileSize = intval(filesize($sFilePath)); else return null;//本地文件不存在 } /*3、检测当前文件是否已经存在于保存目录中,并且文件字节数一致。不一致则保存文件*/ $sNewFile = md5($sFilePath) .'.'. $sExt;//生成将要写入到本地目录的新文件名 if (file_exists($sToPath . $sNewFile) && filesize($sToPath . $sNewFile) == $iFileSize) { return $sNewFile;//文件名称与大小一致,不对载入的文件做保存处理。 } else { /*保存文件*/ $hdlSrc = @fopen($sFilePath, 'rb');//远程文件链接 if ($hdlSrc == false) return null;//源文件最终打开失败 else { $hdlSave = fopen($sToPath . $sNewFile, 'w');//建立本地目标文件链接 $iReadSizeStep = ($iFileSize > 1024*50) ? 1024*50 : 1024*10;//判定每次读取的字节数量. while(!feof($hdlSrc)) { $tmp = fread($hdlSrc, $iReadSizeStep); fwrite($hdlSave, $tmp);//写入读取到的数据 } unset($tmp); fclose ($hdlSrc); fclose ($hdlSave); return $sNewFile;//输出文件名 } } }}?>使用方式:$sTmpFilePath = 读取到数据的文件保存绝对路径;$oTmp = new CInStream();$sUrl = 'http://www.dns.com.cn/';$sFileName = $oTmp->load2File($sUrl, $sTmpFilePath); 一般来说,当你使用一个浏览器访问一个地址正常,但使用程序访问不正常,其原因就是,你程序的http头不包含或包含了不正确的User-Agent。伪造一个User-Agent就可以了。 有curl这么好用的库你不用,去用socket framework入门问题 php火狐怎么读取radio的值! php创建进程的疑惑(pcntl_fork),求解答 正则表达式 数字显示不正常 php中怎样获得请求页的url? 关于工作权限的问题???? PHP的地址彩蛋 一大早,问个关于树型菜单的问题。 我怎么分页处理邮件列表!!! 搭建php调试环境? Zend Studio for Eclipse 和 Zend Studio 有什么区别吗?
$url = parse_url("http://www.webshu.net/Soft/ShowSoftDown.asp?UrlID=1&SoftID=15947");
$fp=fsockopen($url['host'],80,$errno,$errstr);
if($fp){
$header = "GET ".$url['path']."?".$url['query']." HTTP/1.1\r\n";
$header .= "Host: ".$url['host']."\r\n";
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1\r\n";
$header .= "Referer: http://".$url['host']."\r\n";
$header .= "Connection: Close\r\n\r\n";
fwrite($fp, $header);
//$f = fopen($path . basename($file), "wb");
while (!feof($fp))
{
$s = fgets($fp,128);
echo $s;
}
//fclose($jpg);
fclose($fp);根据提示用fsockopen,上面代码结果显示如下:
HTTP/1.1 200 OK Connection: close Date: Wed, 12 Aug 2009 13:20:02 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET Content-Length: 286 Content-Type: text/html Expires: Tue, 11 Aug 2009 13:20:02 GMT Set-Cookie: ASPSESSIONIDSADRDRBR=ILJBCCEBKGPKPBOJPKMCDIDC; path=/ Cache-control: no-cache
我从哪里可以得到真实的文件信息?
thunder://QUFodHRwOi8vZG93bi53ZWJzaHUubmV0L0Rvd25sYW9kL1NDL2N5L2xoMDczMDIwLnJhclpa
AAhttp://down.webshu.net/Downlaod/SC/cy/lh073020.rarZZ用trim和rtrim去掉AA和ZZ后,得出:
http://down.webshu.net/Downlaod/SC/cy/lh073020.rar
<?PHP
/**
* 输入外部流处理类
*
* @author Jerry.Li([email protected])
* @access public
* @package CInStream
*
---------------------------------------------------------------------
- 版本: 0.1
- 文件名:CInStream.class.php
- 编写日期:2009/08/06
- 运行环境:php5以上
- 修改记录:2009/08/06,JerryLi,程序创立
---------------------------------------------------------------------
*//**
* 输入流处理类
* 主要功能是载入外部链接或数据,文本或文件流
*
*/
class CInStream
{
function __construct()
{
if (!function_exists(get_headers))
{
echo '请确认您的版本为PHP5或以上版本,否则程['. __CLASS__.']序无法正常工作。';
exit(0);
}
}
function __destruct()
{
}
/**
* 载入外部文件保存到指定位置
* 特殊处理:如果$sFilePath相同,文件大小相同,的文件存在目标目录,则默认为相同的文件,不写入这个文件。
* 即带缓存的处理方式。
*
* @param string $sFilePath 文件物理路径(可以为本地文件或者远程http上的文件)
* @param string $sToPath 目标文件的保存路径
* @return null / shring 返回保存完成的文件名(md5文件名)
*/
public function load2File($sFilePath, $sToPath)
{
/*1、对传入的文件名进行处理*/
//判断是否远程文件
$sReadType = (strtolower(substr($sFilePath, 0, 4)) == 'http')? 'http' : 'local';
$sExt = preg_replace("/.*\.([^.]+)$/","\\1", $sFilePath);//分离扩展名
$sToPath = str_replace('\\', '/', $sToPath);
$sToPath = (substr($sToPath, -1, 1) != '/') ? $sToPath .'/' : $sToPath;
/*2、检测目标文件是否存在*/
if ($sReadType == 'http')/*检验远程文件是否存在*/
{
$aTmp = @get_headers($sFilePath, 1);
if (!is_array($aTmp) || !strpos($aTmp[0], '200 OK'))
{
unset($aTmp);
return null;//远程文件不存在
}
else
{
$iFileSize = intval($aTmp['Content-Length']);//获取文件大小
$aTmp = explode('/', $aTmp['Content-Type']);//单独处理html方式的扩展名
$sExt = (trim($aTmp[1]) == 'html')? 'html' : $sExt;//取出文件类型
unset($aTmp);
}
}
elseif ($sReadType == 'local')//检验本地文件是否存在
{
if (file_exists($sFilePath))
$iFileSize = intval(filesize($sFilePath));
else
return null;//本地文件不存在
}
/*3、检测当前文件是否已经存在于保存目录中,并且文件字节数一致。不一致则保存文件*/
$sNewFile = md5($sFilePath) .'.'. $sExt;//生成将要写入到本地目录的新文件名
if (file_exists($sToPath . $sNewFile) && filesize($sToPath . $sNewFile) == $iFileSize)
{
return $sNewFile;//文件名称与大小一致,不对载入的文件做保存处理。
}
else
{
/*保存文件*/
$hdlSrc = @fopen($sFilePath, 'rb');//远程文件链接
if ($hdlSrc == false)
return null;//源文件最终打开失败
else
{
$hdlSave = fopen($sToPath . $sNewFile, 'w');//建立本地目标文件链接
$iReadSizeStep = ($iFileSize > 1024*50) ? 1024*50 : 1024*10;//判定每次读取的字节数量.
while(!feof($hdlSrc))
{
$tmp = fread($hdlSrc, $iReadSizeStep);
fwrite($hdlSave, $tmp);//写入读取到的数据
}
unset($tmp);
fclose ($hdlSrc);
fclose ($hdlSave);
return $sNewFile;//输出文件名
}
}
}
}
?>
使用方式:
$sTmpFilePath = 读取到数据的文件保存绝对路径;
$oTmp = new CInStream();
$sUrl = 'http://www.dns.com.cn/';
$sFileName = $oTmp->load2File($sUrl, $sTmpFilePath);