PHP下载远程文件到指定目录
PHP用curl可以轻松实现下载远程文件到指定目录:
<?php
classDownload
{
publicstaticfunctionget($url,$file)
{
returnfile_put_contents($file,file_get_contents($url));
}
publicstaticfunctioncurlGet($url,$file)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_POST,0);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$file_content=curl_exec($ch);
curl_close($ch);
$downloaded_file=fopen($file,'w');
fwrite($downloaded_file,$file_content);
fclose($downloaded_file);
}
publicstaticfunctionopenGet($url,$file)
{
$in=fopen($url,"rb");
$out=fopen($file,"wb");
while($chunk=fread($in,8192))
{
fwrite($out,$chunk,8192);
}
fclose($in);
fclose($out);
}
/**
*
*创建目录,支持递归创建目录
*@paramString$dirName要创建的目录
*@paramint$mode目录权限
*/
publicstaticfunctionsmkdir($dirName,$mode=0777){
$dirs=explode('/',str_replace('\\','/',$dirName));
$dir='';
foreach($dirsas$part){
$dir.=$part.'/';
if(!is_dir($dir)&&strlen($dir)>0){
if(!mkdir($dir,$mode)){
returnfalse;
}
if(!chmod($dir,$mode)){
returnfalse;
}
}
}
returntrue;
}
}
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短