【27】php实现图片上传到七牛云
2018-08-25 本文已影响317人
业余玩家
这里使用的Linux操作系统Ubuntu,包含界面,但整个操作不需要界面环境。Linux系统安装在虚拟机里面,并采用了xshell进行ssh连接操作,比较方便。
安装环境
安装php环境,安装apache,mysql也可以装上,这里暂时不会用到。
sudo apt-get install -y php
sudo apt-get install apache2
启动服务
启动apache服务。
service apache2 start //启动apache服务
service apache2 restart //重新启动apache服务
service apache2 sto'p //关闭apache服务
查看服务是否启动
ps -ef | grep apache

下载官方sdk
当然首先你得注册账号并且创建了存储空间,然后才能进行操作……

下载官方的sdk调用其方法就能够实现图片上传,还有其他的各种操作,然后将这些加入到你的网站代码中就很方便了。
//下载官方sdk
sudo wget https://github.com/qiniu/php-sdk/archive/v7.2.6.tar.gz
//解压到网站目录
sudo tar -xzvf v7.2.6.tar.gz
使用sdk实现功能
依据官方给的开发文档和sdk,你能够很轻松的实现图片上传这个小功能。AccessKey/SecretKey可以在你的个人中心-密钥管理查看。
<html>
<head></head>
<body>
<?php
require "./php-sdk-7.2.6/autoload.php";
use Qiniu\Auth;
$bucket="存储空间";
$accesskey="your accesskey";
$secretkey="your secretkey";
$auth=new Auth($accesskey,$secretkey);
$uptoken=$auth->uploadToken($bucket);
echo $uptoken;
?>
<form method="post" action="http://upload.qiniup.com" enctype="multipart/form-data">
<input name="token" type="hidden" value="<?php echo $uptoken; ?>">
<input name="file" type="file">
<input type="submit" value="上传">
</form>
</body>
</html>
上传表单,上传成功后返回hash值。


最后可以在文件列表查看到你上传的图片,这样就成功了,更多其他的功能可以参考文档进行尝试……
