LeNgHoSt的小窝

I did it my way, ‘Cause it’s my life!

2008年08月17日

PHP远程下载图片并按照原目录保存图片的代码,破解防盗链

这几天发现我写的那个小偷程序用到的主站竟然也开始搞防盗链了,我那小偷程序所有的图片都显示不正常。。。上网查了下资料,写了下面的代码,用到了crul库,还好我的空间支持crul,呵呵!

curl 是使用URL语法的传送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道和大量其他有用的技巧。

下载: pic.php
<?php   
 
/** 
 *
@author LeNgHoSt 
 *
@copyright 2008 
 */
 
 
$pic = $_SERVER['QUERY_STRING'];   
if(!preg_match("/.*\.jpg/", $pic)){   
    
exit('图片读取错误!');   
}   
$fname='pic/'.$pic;   
if(file_exists($fname)){   
    
header('Location: '.$fname);   
}else{   
    
$ch = curl_init();   
    
curl_setopt ($ch, CURLOPT_URL, $pic);   
    
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
    
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.xxx.com'); /*破解防盗链*/ 
    
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)');   
    
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 30);   
    
$file_contents = curl_exec($ch);   
    
curl_close($ch);   
    
echo $file_contents;   
    
$arr=split('/',$fname);   
    
$max=count($arr);   
    
$dir='';   
    
for($i=0;$i<$max-1;$i++){   
        
$dir.=$arr[$i].'/';   
        
if(!file_exists($dir)){   
            @
mkdir($dir,0777);   
        
}   
    
}   
    
$fp=fopen($fname,'w');   
    
if($fp){   
        
fwrite($fp,$file_contents);   
        
fclose($fp);   
    
}   
}   
 
?>

使用方法:pic.php?http://www.xxx.com/xxx.jpg

标签:, , , ,

相关日志

发表评论