zoukankan      html  css  js  c++  java
  • 百度BAE使用ueditor 上传图片

    研究BAE 也有一段时间了,但一直无法解决BAE 上传图片的问题。

    在阅读了

    http://blog.csdn.net/auver/article/details/10103789

    http://www.thinkphp.cn/topic/5825.html

    之后,经过一小段时间的修改,终于配置成功。

      1 <?php
      2     
      3 
      4     /**
      5      * Created by JetBrains PhpStorm.
      6      * User: taoqili
      7      * Date: 12-7-18
      8      * Time: 上午10:42
      9      */
     10     header("Content-Type: text/html; charset=utf-8");
     11     error_reporting(E_ERROR | E_WARNING);
     12     date_default_timezone_set("Asia/chongqing");
     13     include "Uploader.class.php";
     14     //上传图片框中的描述表单名称,
     15     $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
     16     $path = htmlspecialchars($_POST['dir'], ENT_QUOTES);
     17     $globalConfig = include( "config.php" );
     18     $imgSavePathConfig = $globalConfig[ 'imageSavePath' ];
     19 
     20     //获取存储目录
     21     if ( isset( $_GET[ 'fetch' ] ) ) {
     22 
     23         header( 'Content-Type: text/javascript' );
     24         echo 'updateSavePath('. json_encode($imgSavePathConfig) .');';
     25         return;
     26 
     27     }
     28 
     29     //上传配置
     30     $config = array(
     31         "savePath" => $imgSavePathConfig,
     32         "maxSize" => 1000, //单位KB
     33         "allowFiles" => array(".gif", ".png", ".jpg", ".jpeg", ".bmp")
     34     );
     35 
     36     if ( empty( $path ) ) {
     37 
     38         $path = $config[ 'savePath' ][ 0 ];
     39 
     40     }
     41 
     42     //上传目录验证
     43     if ( !in_array( $path, $config[ 'savePath' ] ) ) {
     44         //非法上传目录
     45         echo '{"state":"u975eu6cd5u4e0au4f20u76eeu5f55"}';
     46         return;
     47     }
     48 
     49     $config[ 'savePath' ] = $path . '/';
     50 
     51     //生成上传实例对象并完成上传
     52     $up = new Uploader("upfile", $config);
     53 
     54     /**
     55      * 得到上传文件所对应的各个参数,数组结构
     56      * array(
     57      *     "originalName" => "",   //原始文件名
     58      *     "name" => "",           //新文件名
     59      *     "url" => "",            //返回的地址
     60      *     "size" => "",           //文件大小
     61      *     "type" => "" ,          //文件类型
     62      *     "state" => ""           //上传状态,上传成功时必须返回"SUCCESS"
     63      * )
     64      */
     65     $info = $up->getFileInfo();
     66 
     67     // 上传成功后移动到bcs
     68     function getFileDomain($bucket){
     69         if(!IS_BAE) return '';
     70         return 'http://'.HTTP_BAE_ENV_ADDR_BCS.'/'.strtolower($bucket);
     71     }
     72     function getFileExt($file_name){    // 获取文件扩展名
     73         $extend =explode("." , $file_name);
     74         $va=count($extend)-1;
     75         return $extend[$va];
     76     }
     77     $tempFileName = tempnam(sys_get_temp_dir(),'tp_');
     78     $sourceFileName = $info["url"];
     79     file_put_contents($tempFileName, file_get_contents($sourceFileName));
     80     $fileInfo = pathinfo($sourceFileName);
     81     $srcFile =  $tempFileName;
     82     $ext = getFileExt($sourceFileName);
     83     $fileExt = '.'.$ext; 
     84     $bucket='你自己的bucket';
     85     $savePath = '/'.date('Ymd').'/'. uniqid().$fileExt;
     86     try{
     87         $bcs=new BaiduBCS();
     88         $response=$bcs->create_object($bucket, $savePath,$srcFile,array('acl'=>BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ));
     89         if($response->isOK()){
     90             $srcFile = getFileDomain($bucket) . $savePath;
     91             //echo "<img src='{$srcFile}' /><br/>{$savePath}";
     92         }
     93     }catch(Exception $e){
     94         die('failed');
     95     }
     96     $info["url"] = $srcFile;  // 获取存储在bcs中文件的url,这个也是最后的url
     97     /**
     98      * 向浏览器返回数据json数据
     99      * {
    100      *   'url'      :'a.jpg',   //保存后的文件路径
    101      *   'title'    :'hello',   //文件描述,对图片来说在前端会添加到title属性上
    102      *   'original' :'b.jpg',   //原始文件名
    103      *   'state'    :'SUCCESS'  //上传状态,成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
    104      * }
    105      */
    106 
    107 echo "{'url':'" . $info["url"] . "','title':'" . $title . "','original':'" . $info["originalName"] . "','state':'" . $info["state"] . "'}";
    108                    

    特别注意的是,既然这里返回的是一个完整的url,在ue的配置中就需要配置 “图片修正地址”

    配置修改如下:

    1         UEDITOR_HOME_URL : URL
    2 
    3         //图片上传配置区
    4         ,imageUrl:URL+"php/imageUp.php"             //图片上传提交地址
    5         ,imagePath: " "                             //图片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置
    6         //,imagePath:URL + "php/"                   //图片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置
    7         //,imageFieldName:"upfile"                  //图片数据的key,若此处修改,需要在后台对应文件修改对应参数

    图片修正地址中是一个空格,而不是空。

    欢迎交流。

  • 相关阅读:
    Sprinig.net 双向绑定 Bidirectional data binding and data model management 和 UpdatePanel
    Memcached是什么
    Spring.net 网络示例 codeproject
    jquery.modalbox.show 插件
    UVA 639 Don't Get Rooked
    UVA 539 The Settlers of Catan
    UVA 301 Transportation
    UVA 331 Mapping the Swaps
    UVA 216 Getting in Line
    UVA 10344 23 out of 5
  • 原文地址:https://www.cnblogs.com/tk091/p/3455212.html
Copyright © 2011-2022 走看看