① 网站下载 php网页下载
/*
*功能:php多种方式完美实现下载远程图片保存到本地
*参数:文件url,保存文件名称,使用的下载方式
*当保存文件名称为空时则使用远程文件原来的名称
*/
functiongetImage($url,$filename='',$type=0){
if($url==''){returnfalse;}
if($filename==''){
$ext=strrchr($url,'.');
if($ext!='.gif'&&$ext!='.jpg'){returnfalse;}
$filename=time().$ext;
}
//文件保存路径
if($type){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$img=curl_exec($ch);
curl_close($ch);
}else{
ob_start();
readfile($url);
$img=ob_get_contents();
ob_end_clean();
}
$size=strlen($img);
//文件大小
$fp2=@fopen($filename,'a');
fwrite($fp2,$img);
fclose($fp2);
return$filename;
}
② php curl get 下载远程zip文件保存在本地例子
<?php
if($_POST['submit']){
$url=$_POST['url']; //取得提交过来的地址http://hu60.cn/wap/0wap/addown.php/fetion_sms.zip
$url=urldecode($url);
$fname=basename("$url"); //返回路径中的文件名部分 fetion_sms.zip
$str_name=pathinfo($fname); //以数组的形式返回文件路径的信息
$extname=strtolower($str_name['extension']); //把扩展名转换成小写
//$uptypes=explode(",",$forum_upload); //取得可以上传的文件格式
//$size=getFileSize($url);
$time=date("Ymd",time());
$upload_dir="./upload/";//上传的路径
$file_name=$time.rand(1000,9999).'.'.$fname;
$dir=$upload_dir.$file_name;//创建上传目录
//判断目录是否存在 不存在则创建
if(!file_exists($upload_dir)){
mkdir($upload_dir,0777,true);
}
$contents=curl_download($url,$dir);
if($contents){
echo "下载成功";
}else{
echo "下载失败";
}
}
function curl_download($url, $dir) {
$ch = curl_init($url);
$fp = fopen($dir, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res=curl_exec($ch);
curl_close($ch);
fclose($fp);
return $res;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>远程下载文件</title>
<form name="upform" method="post" action="" enctype='multipart/form-data'>
<input name='url' type='text' size='20'/>
<input type='submit' name='submit' value='远程下载'/>
</form>
</body>
</html>
③ curl获取远程图片时,如何设置本地保存路径
设置保存路径
define('IMAGE_DIR', 'c:\\xampp\\htdocs\\scraper\\image\\');
保存图片函数。
$imageUrl = 你要的图片的url
$imageType = 你要的图片保存的格式
saveImage($imageUrl, $imageType = 'IMAGETYPE_GIF') {
if (!file_exists(IMAGE_DIR)) {
mkdir(IMAGE_DIR, 0777, true);
}
if( $imageType === IMAGETYPE_JPEG ) {
$fileExt = 'jpg';
} elseif ( $imageType === IMAGETYPE_GIF ) {
$fileExt = 'gif';
} elseif ( $imageType === IMAGETYPE_PNG ) {
$fileExt = 'png';
}
$newImageName = md5($imageUrl). '.' . $fileExt;
$image = new Image();
$image->load($imageUrl);
$image->resizeToWidth(100);
$image->save( IMAGE_DIR . $newImageName, $imageType );
return $newImageName;
}
这是我的图片类,保存前可转换格式,图片大小。
<?php
class Image {
private $_image;
private $_imageFormat;
public function load($imageFile) {
$imageInfo = getImageSize($imageFile);
$this->_imageFormat = $imageInfo[2];
if( $this->_imageFormat === IMAGETYPE_JPEG ) {
$this->_image = imagecreatefromjpeg($imageFile);
} elseif( $this->_imageFormat === IMAGETYPE_GIF ) {
$this->_image = imagecreatefromgif($imageFile);
} elseif( $this->_imageFormat === IMAGETYPE_PNG ) {
$this->_image = imagecreatefrompng($imageFile);
}
}
public function save($imageFile, $_imageFormat=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $_imageFormat == IMAGETYPE_JPEG ) {
imagejpeg($this->_image,$imageFile,$compression);
} elseif ( $_imageFormat == IMAGETYPE_GIF ) {
imagegif($this->_image,$imageFile);
} elseif ( $_imageFormat == IMAGETYPE_PNG ) {
imagepng($this->_image,$imageFile);
}
if( $permissions != null) {
chmod($imageFile,$permissions);
}
}
public function getWidth() {
return imagesx($this->_image);
}
public function getHeight() {
return imagesy($this->_image);
}
public function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
public function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
public function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
private function resize($width, $height) {
$newImage = imagecreatetruecolor($width, $height);
imageresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->_image = $newImage;
}
}
?>
④ php代码保存远程图片到本地,出现重复建立文件夹,是怎么回事出现嵌套建立文件夹
没用过php,但如果出现这个问题就一定是if(file_exists($fname))判断出的错,试试将
$fname='yong/'.$id;改成
$fname='/yong/'.$id;试试看
或者要检查的话,就在if(file_exists($fname)){ 上面加一行输出一下file_exists($fname)看看每次判断的结果。