/**
  * 自动远程保存图片
  *
  */
 public function action_saveimg()
 {
     $source = $this->getQuery('src');
     if (!URL::ping($source)) {
         echo $source;
         die;
     }
     $title = trim($this->getQuery('title'));
     $data = @file_get_contents($source);
     $filename = basename($source);
     $extension = strtolower(strtolower(substr(strrchr($filename, '.'), 1)));
     $saveName = str_replace('.', '', microtime(true));
     // 保存的文件名
     $disk = ORM::factory('img_disk')->where('is_use', '=', 1)->find();
     $save_dir = ORM::factory('user', $this->auth['uid'])->save_dir;
     $savefile = Io::strip(Io::mkdir(DOCROOT . $disk->disk_name . '/' . $save_dir) . '/' . $saveName . ".{$extension}");
     $filesize = @file_put_contents($savefile, $data);
     //保存文件,返回文件的size
     if ($filesize == 0) {
         //保存失败
         @unlink($savefile);
         throw new Exception('文件保存到本地目录时失败');
     }
     list($w, $h, $t, ) = getimagesize($savefile);
     $filetype = image_type_to_mime_type($t);
     if (!preg_match('/^image\\/.+/i', $filetype)) {
         throw new Exception('转存文件并非有效的图片文件');
     }
     $category = ORM::factory('img_category')->where('cate_name', '=', '产品搬家')->where('uid', '=', $this->auth['uid'])->find();
     $cate = new Category();
     if (empty($category->cate_name)) {
         $cate_id = $cate->add(array('cate_name' => '产品搬家', 'uid' => $this->auth['uid']));
     } else {
         $cate_id = $category->cate_id;
     }
     $movehomeSession = Session::instance()->get('movehome_id', false);
     if (!empty($title)) {
         if (empty($movehomeSession)) {
             $cate_id = $cate->add(array('cate_name' => $title, 'uid' => $this->auth['uid'], 'parent_id' => $cate_id));
             Session::instance()->set('movehome_id', $cate_id);
         } else {
             $cate_id = $movehomeSession;
         }
     }
     $img = ORM::factory('img');
     $img->picname = $saveName . ".{$extension}";
     $img->filename = $filename;
     $img->filesize = $filesize;
     $img->userid = $this->auth['uid'];
     $img->cate_id = $cate_id;
     $img->disk_id = $disk->disk_domain;
     $img->disk_name = $disk->disk_name . '/' . $save_dir;
     $img->custom_name = $saveName;
     $img->save();
     $url = "http://" . $disk->disk_domain . '.wal8.com/' . $disk->disk_name . '/' . $save_dir . '/' . $saveName . ".{$extension}";
     // 统计数据
     $num = $img->where('userid', '=', $this->auth['uid'])->where('cate_id', '=', $cate_id)->count_all();
     DB::update('img_categories')->set(array('img_num' => $num))->where('uid', '=', $this->auth['uid'])->where('cate_id', '=', $cate_id)->execute();
     $rows = DB::select(array('sum("filesize")', 'total_size'), array('count("userid")', 'total_num'))->from('imgs')->where('userid', '=', $this->auth['uid'])->execute()->current();
     DB::update('users')->set(array('use_space' => $rows['total_size'], 'count_img' => $rows['total_num']))->where('uid', '=', $this->auth['uid'])->execute();
     $result = array('status' => true, 'source' => $source, 'url' => $url);
     echo $url;
     $this->auto_render = false;
 }
$info = DB::select()->from('imgup_movestore')->where('status', '=', 5)->order_by('id', 'DESC')->limit(1)->execute()->current();
# 获取config信息
$config = DB::select('id', 'disk_domain', 'disk_name')->from('img_disks')->where('is_use', '=', 1)->execute()->current();
$disk_id = $config['disk_domain'];
$disk_name = $config['disk_name'];
if (!empty($info)) {
    $info['id'] = (int) $info['id'];
    $info['uid'] = (int) $info['uid'];
    $save_dir = ORM::factory('user', $info['uid'])->save_dir;
    $csvFile = 'src_csv/' . $info['uid'] . '/' . $info['src_file'];
    $url = 'http://' . $disk_id . '.wal8.com/' . $disk_name . '/' . $save_dir . '/';
    $savePath = 'dest_csv/' . $info['uid'] . '/';
    $saveFilename = time() . '_' . rand() . '.csv';
    # 创建目录
    Io::mkdir('../' . $savePath);
    Io::mkdir('../' . $disk_name . '/' . $movestore['uid'] . '/');
    $content = changeCharacter('../' . $csvFile);
    #搬家完毕的图片
    $list = DB::select('m.url', array('imgs.disk_id', 'disk_domain'), 'imgs.userid', 'imgs.picname', array('imgs.disk_name', 'img_dir'))->from(array('store_imgs', 'm'))->join('imgs')->on('imgs.id', '=', 'm.img_id')->where('m.status', '=', '1')->where('m.sid', '=', $info['id'])->execute()->as_array();
    # 存在搬家完毕的图片生成新的CSV文件,不然就改变状态为3即该文件不存在图片或者该文件的图片不能访问
    if (!empty($list)) {
        foreach ($list as $item) {
            $url = "http://" . $item['disk_domain'] . '.wal8.com/' . $item['img_dir'] . '/' . $item['picname'];
            $content = str_replace($item['url'], $url, $content);
            #DB::delete('store_imgs')->where('sid', '=', $completeStore['id'])->where('status', '=', '1')
            #    ->where('img_id', '=', $item['id']);
        }
        $content = iconv("utf-8", "UCS-2LE", $content);
        @file_put_contents('../' . $savePath . $saveFilename, $content);
        //file_put_contents('../'. $savePath . $saveFilename, "\xFF\xFE" . mb_convert_encoding($content, 'UTF-16LE', 'UTF-8'));
        DB::update('imgup_movestore')->set(array('status' => 1, 'finish_time' => date('Y-m-d H:i:s'), 'dest_file' => $saveFilename))->where('id', '=', $info['id'])->execute();
 /**
  * 自动远程保存图片
  *
  */
 public function action_saveimg()
 {
     $source = $this->getQuery('src');
     if (!URL::ping($source)) {
         echo $source;
         die;
     }
     $savePath = "books_img/" . $this->auth['uid'] . '/';
     Io::mkdir(DOCROOT . $savePath);
     $data = @file_get_contents($source);
     $filename = basename($source);
     $extension = strtolower(strtolower(substr(strrchr($filename, '.'), 1)));
     $saveName = str_replace('.', '', microtime(true)) . '.' . $extension;
     // 保存的文件名
     $filename = $savePath . $saveName;
     $filesize = @file_put_contents(DOCROOT . $filename, $data);
     //保存文件,返回文件的size
     if ($filesize == 0) {
         //保存失败
         @unlink($savefile);
         throw new Exception('文件保存到本地目录时失败');
     }
     list($w, $h, $t) = getimagesize(DOCROOT . $filename);
     $filetype = image_type_to_mime_type($t);
     if (!preg_match('/^image\\/.+/i', $filetype)) {
         throw new Exception('转存文件并非有效的图片文件');
     }
     $filename = $this->thumb->create($filename, 640, 640);
     echo URL::domain() . $filename;
     $this->auto_render = false;
 }