欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

upyun图片fetch的简单封装

程序员文章站 2022-04-11 08:04:15
...
php代码:
<?php
header("Content-type: text/html; charset=utf-8");
require_once('./upyun.class.php');
$upyun = new UpYun('空间', '账户', '密码');
$root = '根目录';

function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   $s =  ((float)$usec + (float)$sec);
   return  str_replace('.','',$s);
}
function fetch($u){
	try {
	    global $upyun,$root;
	    $fh = file_get_contents($u);
		//$fh = 'var newsClose = '.$fh;
		$imgPath = $root.'/'.date('Y').'/'.date('m').'/'.date('d').'/'.microtime_float().'.png';
		
		//die();

	    $rsp = $upyun->writeFile($imgPath, $fh, True);   // 上传图片,自动创建目录
		return $imgPath;
	    //fclose($fh);
	    
	}
	catch(Exception $e) {
	    echo $e->getCode();
	    echo $e->getMessage();
	}
	
}
fetch('http://res.fuliqu.com/bbs/upload/forum/cj/2014/03/14/13947880739402.png');