コード例 #1
0
ファイル: csvimport_facade.php プロジェクト: herrify/iwebshop
 /**
  * @brief 开始运行
  */
 public static function run()
 {
     set_time_limit(0);
     ini_set("max_execution_time", 0);
     $csvType = IReq::get('csvType');
     $category = IFilter::act(IReq::get('category'), 'int');
     $pluginDir = IWeb::$app->getBasePath() . 'plugins/csvPacketHelper/';
     if (!file_exists($pluginDir)) {
         die('此功能仅供授权版本使用,请您购买商业授权');
     }
     if (!class_exists('ZipArchive')) {
         die('服务器环境中没有安装zip扩展,无法使用此功能');
     }
     if (extension_loaded('mbstring') == false) {
         die('服务器环境中没有安装mbstring扩展,无法使用此功能');
     }
     //处理上传
     $uploadInstance = new IUpload(9999999, array('zip'));
     $uploadCsvDir = 'runtime/cvs/' . date('YmdHis');
     $uploadInstance->setDir($uploadCsvDir);
     $result = $uploadInstance->execute();
     if (!isset($result['csvPacket'])) {
         die('请上传指定大小的csv数据包');
     }
     if (($packetData = current($result['csvPacket'])) && $packetData['flag'] != 1) {
         $message = $uploadInstance->errorMessage($packetData['flag']);
         die($message);
     }
     $zipPath = $packetData['fileSrc'];
     $zipDir = dirname($zipPath);
     $imageDir = IWeb::$app->config['upload'] . '/' . date('Y/m/d');
     file_exists($imageDir) ? '' : IFile::mkdir($imageDir);
     //解压缩包
     $zipObject = new ZipArchive();
     $zipObject->open($zipPath);
     $isExtract = $zipObject->extractTo($zipDir);
     $zipObject->close();
     if ($isExtract == false) {
         $message = '解压缩到目录' . $zipDir . '失败!';
         die($message);
     }
     //实例化商品
     $goodsObject = new IModel('goods');
     $photoRelationDB = new IModel('goods_photo_relation');
     $photoDB = new IModel('goods_photo');
     $cateExtendDB = new IModel('category_extend');
     //获得配置文件中的数据
     $config = new Config("site_config");
     $dirHandle = opendir($zipDir);
     while ($fileName = readdir($dirHandle)) {
         if (strpos($fileName, '.csv') !== false) {
             //创建解析对象
             switch ($csvType) {
                 case "taobao":
                     include_once $pluginDir . 'taoBaoPacketHelper.php';
                     $helperInstance = new taoBaoPacketHelper($zipDir . '/' . $fileName, $imageDir);
                     $titleToCols = taoBaoTitleToColsMapping::$mapping;
                     break;
                 default:
                     $message = "请选择csv数据包的格式";
                     die($message);
             }
             //从csv中解析数据
             $collectData = $helperInstance->collect();
             //插入商品表
             foreach ($collectData as $key => $val) {
                 $collectImage = isset($val[$titleToCols['img']]) ? $val[$titleToCols['img']] : '';
                 //有图片处理
                 if ($collectImage) {
                     //图片拷贝
                     $_FILES = array();
                     foreach ($collectImage as $image) {
                         foreach ($image as $from => $to) {
                             if (!is_file($from)) {
                                 continue;
                             }
                             IFile::xcopy($from, $to);
                             //构造$_FILES全局数组
                             $_FILES[] = array('size' => 100, 'tmp_name' => $to, 'name' => basename($to), 'error' => 0);
                         }
                     }
                     //调用文件上传类
                     $photoObj = new PhotoUpload();
                     $uploadImg = $photoObj->run(true);
                     $showImg = current($uploadImg);
                 }
                 //处理商品详情图片
                 $toDir = IUrl::creatUrl() . dirname($to);
                 $goodsContent = preg_replace("|src=\".*?(?=/contentPic/)|", "src=\"{$toDir}", trim($val[$titleToCols['content']], "'\""));
                 $insertData = array('name' => IFilter::act(trim($val[$titleToCols['name']], '"\'')), 'goods_no' => goods_class::createGoodsNo(), 'sell_price' => IFilter::act($val[$titleToCols['sell_price']], 'float'), 'market_price' => IFilter::act($val[$titleToCols['sell_price']], 'float'), 'up_time' => ITime::getDateTime(), 'create_time' => ITime::getDateTime(), 'store_nums' => IFilter::act($val[$titleToCols['store_nums']], 'int'), 'content' => IFilter::addSlash($goodsContent), 'img' => isset($showImg['img']) ? $showImg['img'] : '', 'seller_id' => self::$seller_id);
                 $goodsObject->setData($insertData);
                 $goods_id = $goodsObject->add();
                 //处理商品分类
                 if ($category) {
                     foreach ($category as $catId) {
                         $cateExtendDB->setData(array('goods_id' => $goods_id, 'category_id' => $catId));
                         $cateExtendDB->add();
                     }
                 }
                 //处理商品图片
                 if ($uploadImg) {
                     $imgArray = array();
                     foreach ($uploadImg as $temp) {
                         if (isset($temp['img']) && $temp['img']) {
                             $imgArray[] = $temp['img'];
                         }
                     }
                     if ($imgArray) {
                         $photoData = $photoDB->query('img in ("' . join('","', $imgArray) . '")', 'id');
                         if ($photoData) {
                             foreach ($photoData as $item) {
                                 $photoRelationDB->setData(array('goods_id' => $goods_id, 'photo_id' => $item['id']));
                                 $photoRelationDB->add();
                             }
                         }
                     }
                 }
             }
         }
     }
     //清理csv文件数据
     IFile::rmdir($uploadCsvDir, true);
     die('<script type="text/javascript">parent.artDialogCallback();</script>');
 }
コード例 #2
0
ファイル: adminmethod.php プロジェクト: snamper/xiaoshuhaochi
 function debkfile()
 {
     $dirname = IReq::get('dirname');
     if (empty($dirname)) {
         $this->message('文件名为空');
     }
     IFile::clearDir(hopedir . '/dbbak/' . $dirname);
     IFile::rmdir(hopedir . '/dbbak/' . $dirname);
     $this->success('操作成功! ');
 }