Home  >  Article  >  php教程  >  upyun图片fetch的简单封装

upyun图片fetch的简单封装

PHP中文网
PHP中文网Original
2016-05-25 16:59:371058browse

php代码:

<?php
header("Content-type: text/html; charset=utf-8");
require_once(&#39;./upyun.class.php&#39;);
$upyun = new UpYun(&#39;空间&#39;, &#39;账户&#39;, &#39;密码&#39;);
$root = &#39;根目录&#39;;

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

	    $rsp = $upyun->writeFile($imgPath, $fh, True);   // 上传图片,自动创建目录
		return $imgPath;
	    //fclose($fh);
	    
	}
	catch(Exception $e) {
	    echo $e->getCode();
	    echo $e->getMessage();
	}
	
}
fetch(&#39;http://res.fuliqu.com/bbs/upload/forum/cj/2014/03/14/13947880739402.png&#39;);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn