我是一个PHP小小白,最近学习了一下如何用PHP操作OSS,记录一下自己的学习心得。不足的地方,大家别见笑。
笔记一:如何展示自己的bucket列表:
<?php
/**
* 加载sdk包以及错误代码包
*/
require_once 'oss_php_sdk/sdk.class.php';
$oss_sdk_service = new ALIOSS();
$bucket_list = $oss_sdk_service->list_bucket();
$doc = new DOMDocument();
$doc->loadXML($bucket_list->body);
echo "<p><b>My bucket list:</b></p>";
echo "<ul>";
$buckets = $doc->getElementsByTagName("Bucket");
foreach( $buckets as $bucket )
{
$names = $bucket->getElementsByTagName( "Name" );
$name = $names->item(0)->nodeValue;
$ctimes = $bucket->getElementsByTagName( "CreationDate" );
$ctime = $ctimes->item(0)->nodeValue;
echo "<li>$name [create time: $ctime]</li>\n";
}
echo "</ul>";
?>
<html>
<body>
<form action="upload_img.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
/**
* 加载sdk包以及错误代码包
*/
require_once 'oss_php_sdk/sdk.class.php';
$oss_sdk_service = new ALIOSS();
$bucket = ' 你的bucket名字';
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " KB<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
$content = '';
$length = 0;
$fp = fopen($_FILES["file"]["tmp_name"],'r');
if($fp)
{
$f = fstat($fp);
$length = $f['size'];
while(!feof($fp))
{
$content .= fgets($fp,8192);
}
}
$upload_file_options = array('content' => $content, 'length' => $length);
$upload_file_by_content = $oss_sdk_service->upload_file_by_content($bucket, $_FILES["file"]["name"], $upload_file_options);
$img_url = "http://storage.aliyun.com/" . $bucket . "/" . $_FILES["file"]["name"];
echo "Upload successfully! The OSS URL of this file: " . $img_url . "<br />";
echo "If the bucket is public-read, the uploaded image can be shown as:" . "<br />";
echo "<img src=$img_url />";
}
}
else
{
echo "Invalid file";
}
?>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。