开发者社区> 问答> 正文

PHP上传图片OSS同步更新

刚做了个效果,可以实现PHP上传图片,本地服务器保留一份,OSS上同步更新
先上效果
1、PHP的上传表单页面


2、本地服务器的上传目录,可以看到上传后的图片




3、OSS上的bucket目录,同步更新文件



展开
收起
dengwei1999 2014-08-05 16:14:32 18220 0
5 条回答
写回答
取消 提交回答
  • RePHP上传图片OSS同步更新
    php的   图片缩略  只有在上传完成之后才能进行缩略吗  
    2015-01-17 08:20:08
    赞同 展开评论 打赏
  • RePHP上传图片OSS同步更新
    写得很乱
    2014-10-19 10:32:33
    赞同 展开评论 打赏
  • 欢迎进行技术分享,感谢你的支持!
    2014-08-30 01:31:43
    赞同 展开评论 打赏
  • web前端开发与设计
    但是小白的我看不懂,希望出一整套源码代码 以多文件形式传上来
    2014-08-11 19:50:05
    赞同 展开评论 打赏
  • RePHP上传图片OSS同步更新
    下面附上我的代码,PHP用的是Thinkphp3.2的框架
    public function add(){
            if(IS_POST){
                $oss_sdk_service = new \ALIOSS();
                //设置是否打开curl调试模式
                $oss_sdk_service->set_debug_mode(true);
                $news_mod = D('News');
                $data ['title'] =  I('post.title','','htmlspecialchars');
                $data['ord'] = 0;
                $data['addtime'] = time();
                $data['status'] = 0;
                if (isset($_FILES['pic'])){
                    //文件上传配置,可以生成缩略图
                    $date11 =date('Ymd');
                    $upload = new \Think\UploadFile();// 实例化上传类
                    $upload->maxSize  = 80000000 ;// 设置附件上传大小
                    $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                    $upload->allowTypes = array('image/jpg','image/jpeg','image/pjpeg','image/png','image/gif');
                    $upload->savePath =  './Upload/news/'.$date11.'/';// 设置附件上传目录
                    $upload->saveRule = 'uniqid';
                    //缩略图配置
                    $upload->thumb = true;
                    $upload->thumbMaxWidth = '300';
                    $upload->thumbMaxHeight = '380';
                    $upload->thumbPrefix = '';
                    $upload->thumbSuffix ='.300';
                    $upload->thumbRemoveOrigin = false;
                    //开始上传
                    if($upload->upload()){
                        $info =  $upload->getUploadFileInfo();
                        //大图
                        $data['picurl'] =  '/Upload/news/'.$date11.'/'.$info[0]['savename'];
                        //上传到OSS
                        $picurl ='./Upload/news/'.$date11.'/'.$info[0]['savename'];
                        //缩略图
                        $thumbpic = getThumb($picurl,6);
                        $thumbfilename = end ( explode ( '/', $thumbpic ) );;
                        upload_by_file($oss_sdk_service,'Upload/news/'.$date11.'/'.$info[0]['savename'], $picurl);
                        upload_by_file($oss_sdk_service,'Upload/news/'.$date11.'/'.$thumbfilename, $thumbpic);
                    }
                }
                $result = $news_mod->add($data);
                if(false !== $result){
                    $this->success ( '新增成功!',  U('Home/Index/add') );
                }
                else{
                    $this->error ( '新增巡失败!' );
                }
            }else{
                $this->display();
            }
        }

    html代码
    <form action="" method="post" enctype="multipart/form-data" name="form1">
      <table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td height="30" colspan="2" align="center">上传图片</td>
        </tr>
        <tr>
          <td width="140" height="30" align="right">标题:</td>
          <td width="460" height="30"><input type="text" name="title"></td>
        </tr>
        <tr>
          <td height="30" align="right">上传图片:</td>
          <td height="30"><input type="file" name="pic"></td>
        </tr>
        <tr>
          <td height="30" align="right"> </td>
          <td height="30"><input type="submit" name="Submit" value="提交"></td>
        </tr>
      </table>
    </form>

    -------------------------

    RePHP上传图片OSS同步更新
    //获取图片的缩略图名称

    function getThumb($image,$lx) {
        $filename = end ( explode ( '/', $image ) );
        $filename1=explode('.',$filename);
        if ($filename1) {
            if($lx==1){
                $lxtt = '.120.';
            }else if($lx==2){
                $lxtt = '.150.';
            }else if($lx==3){
                $lxtt = '.180.';
            }else if($lx==4){
                $lxtt = '.200.';
            }else if($lx==5){
                $lxtt = '.280.';
            }else if($lx==6){
                $lxtt = '.300.';
            }
            $files = $filename1[0].$lxtt.$filename1[1];
            $newfile=str_replace($filename,$files,$image);
            return $newfile;
        }
    }

    -------------------------

    RePHP上传图片OSS同步更新
    希望这个帖子能加金啊

    -------------------------

    RePHP上传图片OSS同步更新
    加个函数upload_by_file()
    //通过路径上传文件
    function upload_by_file($obj,$object,$file_path){
        $bucket = 'dengwei1999';
       // $object = '115001_1313.jpg';
       // $file_path = "../pic/115001_1313.jpg";
        $response = $obj->upload_file_by_file($bucket,$object,$file_path);
       // _format($response);
    }
    2014-08-05 16:16:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载