« php 删除字符串中HTML标签的函数PHP sleep() time_sleep_until() 函数 延时或暂停程序 »

php 下载远程图片 的几种方法

从网上看看了一些php下载文件的资料,改了改,写了这么个函数.

$url = "下载文件的地址";
$sgff = "要保存文件的本地目录";
$filename = "要保存为什么样的文件名";

$name= array_pop(explode(".", $url));  //获取后缀名'
$filename = $filename.".".$name;    //文件名后加入后缀'

function check_url($url){
 return preg_match("/^(http|ftp)(:\/\/)([a-zA-Z0-9-_]+[\.\/]+[\w\-_\/]+.*)+$/i", $url);
}
function down($url,$sgff,$filename){
 error_reporting(0);
 set_time_limit(0);
 $argv[1] = $url;
 $argv[2] = $sgff;
 //无参数则给出提示
 if (empty($argv[1])){
  echo "Usage: ". $argv[1] ." URL [Destination]\n\n";
  exit();
 }

 //设置获取基本变量
 $url = $argv[1];
 echo "<b>Download
Url:</b>".$url."<br>";
 $save_path = $argv[2] ? $argv[2] : "./";
 $file_name = $filename;
 $localfile = $save_path.$file_name;
 echo "<b>File path:</b>".$localfile."<br>";
 //检查变量
 if (!check_url($url)){
  exit("<b>Error: URL </b>". $url ." <b>invalid.</b>\n\n");
 }
 if (file_exists($localfile)){
  exit("<b>Error: local file </b>". $localfile ." <b>exists.</b>\n\n");
 }
 //打开远程文件
 $fp = fopen($url, "rb");
 if (!$fp){
  exit("<b>Error: Download </b>". $url ." <b>failed.</b>\n\n");
 }

 //打开本地文件
 $sp = fopen($localfile, "wb");
 if (!$sp){
  exit("<b>Error: Open local file </b>". $localfile ." <b>failed.</b>\n\n");
 }

 //下载远程文件
 echo "Downloading, please waiting...\n\n";
 while (!feof($fp)){
  $tmpfile .= fread($fp, 512);
 }

 //保存文件到本地
 fwrite($sp, $tmpfile);
 fclose($fp);
 fclose($sp);
 echo "Download file ". $file_name ." succeed!\n\n";

}

down($url,$sgff,$filename);

 

<?php
$img = file_get_contents('http://www.baidu.com/img/baidu_logo.gif');
file_put_contents('1.gif',$img);
echo '<img src="1.gif">';
?>

稍复杂方法

<?php
function GrabImage($url,$filename="") {
if($url==""):return false;endif;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;endif;
$filename=date("dMYHis").$ext;
}
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;
}
$img=GrabImage("http://www.baidu.com/img/baidu_logo.gif",""); //地址
if($img):echo '<pre><img src="'.$img.'"></pre>';else:echo "false";endif;
?>

 

 

<?php

//
// Function: 获取远程图片并把它保存到本地
//
//
// 确定您有把文件写入本地服务器的权限
//
//
// 变量说明:
// $url 是远程图片的完整URL地址,不能为空。
// $filename 是可选变量: 如果为空,本地文件名将基于时间和日期
// 自动生成.

function GrabImage($url,$filename="") {
if($url==""):return false;endif;

if($filename=="") {
    $ext=strrchr($url,".");
    if($ext!=".gif" && $ext!=".jpg"):return false;endif;
    $filename=date("dMYHis").$ext;
}

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;
}


$img=GrabImage("http://news.bbc.co.uk/images/_1978837_detector_ap100.jpg","");
if($img):echo '<pre><img src="'.$img.'"></pre>';else:echo "false";endif;  

 


?>

dedecms中的:

       if(!empty($saveremoteimg))
        {
                $body = stripslashes($body);
                $img_array = array();
                preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array);
                $img_array = array_unique($img_array[2]);
                set_time_limit(0);
                $imgUrl = $img_dir."/".strftime("%Y%m%d",time());
                $imgPath = $base_dir.$imgUrl;
                $milliSecond = strftime("%H%M%S",time());
                if(!is_dir($imgPath)) @mkdir($imgPath,0777);
                foreach($img_array as $key =>$value)
                {
                        $value = trim($value);
                        $get_file = @file_get_contents($value);
                        $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
                        $fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3);
                        if($get_file)
                        {
                                $fp = @fopen($rndFileName,"w");
                                @fwrite($fp,$get_file);
                                @fclose($fp);
                        }
                        $body = ereg_replace($value,$fileurl,$body);
                }
                $body = addslashes($body);
        }

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

网站目录

最近发表

最新评论及回复

文章归档

Search

Powered By Z-Blog 1.8 Terminator(beta) Build 71218 Designed by Michael

Copyright 0-9999 subin.org.cn Rights Reserved. 晋ICP备08000685号