废话不多说,直接重点
在新浪storage下面创建一个domain,名字叫什么随便,我的叫test
能收到这篇文章的人,估计下面的不解释了
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<title>上传测试</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="newpost.php" enctype="multipart/form-data">
图片:<input type="file" name="photo" />
<input type="submit" value="提交" />
</form>
</body>
</html>
下面要做的就是new一个SaeStorage();对象,然后步骤1,更改文件名字,存入,拿出URL,根据URL访问图片,完了
为了增长文章长度,把新浪的这个对象的方法全部copy过来
PHP调用方式
用户需要先创建一个Domain,在线管理平台=>服务管理=>Storage,输入Domain名称,创建一个Domain
在线管理平台=>服务管理=>Storage,确认删除某个Domain
请参考,http://sae.sina.com.cn/?m=devcenter&catId=29#anchor_c267049d9b7e52eeb331ec37381812be
目前Storage提供以下的函数支持:
void __construct ([string $_accessKey = ''], [string $_secretKey = ''])
bool delete (string $domain, string $filename)
string errmsg ()
int errno ()
bool fileExists (string $domain, string $filename)
检查文件是否存在
array getAttr (string $domain, string $filename, [array $attrKey = array()])
获取文件属性
array getList (string $domain, [string $prefix = '*'], [int $limit = 10], [int $skip = 0])
获取指定domain下的文件名列表
string getUrl (string $domain, string $filename)
取得访问存储文件的url
mixxed read (string $domain, string $filename)
获取文件的内容
void setAuth (string $akey, string $skey)
设置key
bool setDomainAttr (string $domain, [array $attr = array()])
设置Domain属性
bool setFileAttr (string $domain, string $filename, [array $attr = array()])
设置文件属性
string upload (string $domain, string $destFile, string $srcFile, [array $attr = array()],
[bool $compress = false])
将文件上传入存储
string write (string $domain, string $destFile, string $content, [int $size = -1], [array
$attr = array()], [bool $compress = false])
将数据写入存储
具体的使用说明请参考,http://apidoc.sinaapp.com/sae/SaeStorage.html
cpoy完毕,贴我自己的代码
<?php
文件名叫newpost.php
function backImageUrl($photo){
$typearr = array("image/jpeg","image/jpg","image/pjpeg","image/gif","image/bmp");
if(!in_array($photo["type"],$typearr)){
header("Content-Type:text/html;charset=UTF-8");
$url = @$_SERVER["HTTP_REFERER"];
header("refresh:3;url={$url}");
exit("上传格式只能是jpeg,jpg,pjpeg,gif,bmp");
}
$suffixname = strrchr($photo["name"],'.');
$s = new SaeStorage();
$s->upload( 'test' ,$photo["tmp_name"].$suffixname,$photo["tmp_name"] );
$erjin = $s->read( 'test' ,$photo["tmp_name"]) ;
return $s->getUrl( 'test' ,$photo["tmp_name"]).$suffixname;
}
echo backImageUrl($_FILES["photo"]);
?>
m.kuanggouli.cn 推广返充值