Home  >  Article  >  Backend Development  >  SAE-上传本map片到SAE的Storage(php版)

SAE-上传本map片到SAE的Storage(php版)

WBOY
WBOYOriginal
2016-06-13 12:22:30974browse

SAE-上传本地图片到SAE的Storage(php版)

新浪的SAE处于安全期间,不支持直接将本地文件上传的SAE。


 

也就是无法通过选择文件按钮选择本地的文件,点击提交之后,文件不能成功提交到SAE云服务器的,那怎么办哪??需要通过SAE提供的Storage来实现,通过SotrageAPI接口将本地文件上传的SAEStorage,然后再通过访问Storage里的文件来实现这一效果!!


下面通过图片来一步步解说:

1. 打开我们的SAE应用在服务管理这里可以看到storage


 

2. 然后点击storage进去之后看到下面界面

 

3. 点击新建domain名字随便起,这个domain用来存放将来我们上传的文件,创建好之后可以点击domain管理查看我们创建的domain

 

 

4. 一切具备,现在只欠我们通过写程序往这domain里存放数据了,本人用的是php语言,其他语言原理也一样!

 

 

<?php // 当用户点击submit提交上传的文件时if(isset($_POST["submit"])){	// 创建SAE storage存储	$storage= new SaeStorage();// 创建SAE storage存储对象	$domain = &#39;kepuna&#39;;// 这里的$domain对应得名字就是自己起的名字		$fileType = $_FILES["file"]["type"]; //被上传文件的类型	if(($fileType=="image/gif") || ($fileType=="image/jpeg")||($fileType=="image/jpg")||($fileType=="image/png")){		if($storage->fileExists($domain,$filename) == true) {// 判断文件是否已经存在        echo "<p style="'background:#FCC9C4;border-radius:">图片已存在,请重新上传!</p>";        }	else{			$filename = $_FILES["file"]["name"];	$storage->upload( $domain,$filename,$_FILES[file][tmp_name]);         echo "<p style="'background:#7CBD55;border-radius:">图片上传成功!</p>";        echo "<script> window.location=&#39;showImage.php&#39;;</script>";           <span style="white-space:pre">	</span>}    }else{    	echo "<p style="'background:#FCC9C4;border-radius:">图片格数不正确,上传失败!</p>";    }}?>	<title></title>	<meta charset="utf-8">	<meta name="viewport" content="width=device-width,initial-scale=1">	<link href="./css/style.css" rel="stylesheet" type="text/css" media="all">				

看下效果图:有点卡大家可以把代码考自己机子上运行下



我们怎么通过程序访问这个我们存放进domain里的图片那?

接下来是showImage.php的内容

<?php $sae_storage = new SaeStorage();$domainName = "kepuna";$listArray = $sae_storage->getList($domainName);        foreach($listArray as $image){    echo "<img  src="'http://hjmshop-kepuna.stor.sinaapp.com/%24image'/" alt="SAE-上传本map片到SAE的Storage(php版)" >";}?>


方法还有很多种,我这只是其中一种,可以参考

http://apidoc.sinaapp.com/class-SaeStorage.html 这个SAE的官方文档大家自己摸索下,很简单!

下面是我domain中的所有图片

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