标签:des ar io os 使用 sp for on 文件
<?php /* Description: FlashPaper文档转换,转换可打印的文档为FLASH格式并保存转换的相关信息到log_swfread表 * 使用FlashPaper服务需求 1. 运行在Windows(xp/2003)服务器下 2. 能调用系统命令exec 3. 调用的文件目录必须可读可写 4. 需要转换的文件类型,服务器必须安装相应的转换软件pdf安装 pdf 9.0, office安装2007,且必须是正版 5. 服务器需要支持虚拟打印机功能 6. 服务器的内容要足够大,最少4GB 7. 在系统管理-站点管理-设置网站URL和访问URL一致 * 测试FlashPaer 1.运行初始化中 初始化.bat文件 无任何错误即可(xp/2003) 2.查看打印机文档中是否有打印机FlashPaer 3.运行FlashPrinter.exe直接讲要转换的文件拖到此,如果能转换则软件安装无问题 * API调用方法 1. 同步调用 -支持多文件同时转换,不支持执行时间检测和转换状态检测 $flashPaper = A(‘Api.FlashPaper‘); $flashPaper->convertFile($uploadlist); 2. 异步调用 -多文件上传需调用多次,支持执行时间检测和转换状态更新 $url = $this->config[‘site_url‘].‘Api/FlashPaper/convertFile‘; foreach($uploadList as $data){ post($url,$data); } * 注意 1. 多文件同时转换 - FlashPaper产生多个独立的进程,转换完毕后自动关闭经常和被调用的程序(尚未支持) 2. 使用pptx/docx/xlsx转换速度快于2003格式 * 保持转换信息 - 系统默认保存转换的文件名称,文件大小,文件类型,转换后保持地址,转换时间,转换运行时间和转换失败的错误信息 - 默认保存的数据库表是log_swfread 保存的方式是execSaveInfo */ class FlashPaperAction extends Action { public $flashpaper_url = ‘/Public/FlashPaper/FlashPrinter.exe‘; // 应用程序路径 public $allow_file_ext = ‘doc|docx|ppt|pptx|xls|xlsx|pdf|jpg|bmp|gif|png|txt‘; // 允许转换的文件格式(可打印的文件) public $swf_savepath = ‘/Data/swfscorce/‘; // SWF保存虚拟路径 public $time_limit = 1000; // 程序最大运行时间 private $socket = false; // 是否socket请求 private $scorce_file_path; // 上传文件物理路径 private $scorce_save_path; // 上传文件的保存路径 private $score_file_name; // 上传文件保存名称 private $scorce_file_ext; // 上传文件扩展名 private $scorce_file_size; // 文件大小 private $exec_error; // 转换文件错误提示 private $exec_start_time; // 转换开始时间 private $exec_end_time; // 转换结束时间 private $scorce_attch_id; // 附件信息ID号 private $swf_file_path = ‘‘; // 转换swf文件保存物理路径 public function test(){ // 测试各种格式的文件转换 // 支持的文件: txt pdf ppt pptx jpg gif png bmp xls // 正在测试的文件: docx,xlsx,rtf pps pot doc $command = ‘D:/yjoa/Public/FlashPaper/FlashPrinter.exe -o D:/yjoa/Data/swfScorce/201110/1.docx.swf D:/yjoa/Data/flashpaper/201110/1.xlsx‘; $command = str_replace("/","\\",$command); $exec_start_time = $this->exectime(); exec($command, $return_array); $exec_end_time = $this->exectime(); echo ‘Execue Time:‘.round($exec_end_time - $exec_start_time, 6).‘<br/>‘; echo $command; } // 转换DEMO页面 public function _empty() { $fconfig = F(‘Config‘); $upload_allow_ext = ‘‘; $upload_allow_desc = ‘‘; $upload_allow_extsarr = explode(‘,‘, $fconfig[‘upload_allow_exts‘]); foreach ($upload_allow_extsarr as $v) { $upload_allow_ext = $upload_allow_ext. ‘*‘.$v.‘;‘; $upload_allow_desc = $upload_allow_desc. ‘.‘.$v.‘,‘; } $upload_allow_ext = substr($upload_allow_ext, 0, -1); $upload_allow_desc = substr($upload_allow_desc, 0, -1); $this->assign(‘upload_allow_desc‘, $upload_allow_desc); $this->assign(‘upload_allow_ext‘, $upload_allow_ext); $this->display(‘read‘); } /* 初始化 */ public function _initialize() { ignore_user_abort(true); set_time_limit($this->time_limit); // FLASHPAPER转换程序物理地址 $this->flashpaper_url = $_SERVER[‘DOCUMENT_ROOT‘].$this->flashpaper_url; // 自动创建转换保存目录 @mkdir(‘.‘.$this->swf_savepath); @mkdir(‘.‘.$this->swf_savepath.date(‘Ym‘)); // SWF文件保存虚拟路径 $this->swf_file_path = $this->swf_savepath.date(‘Ym‘); // SWF文件保存物理路径 $this->swf_savepath = $_SERVER[‘DOCUMENT_ROOT‘].$this->swf_savepath.date(‘Ym‘).‘/‘; } /* 调用FlashPaper,$uploadlist是ThinkPHP的UPLOAD上传的数组形式的文件数组信息 */ public function convertFile($uploadList) { if (!$uploadList){ $uploadList = array( 0 => $_POST); $this->socket = true; } if(is_array($uploadList)) { foreach ($uploadList as $v) { $this->scorce_file_path = $_SERVER[‘DOCUMENT_ROOT‘].substr($v[‘savepath‘],1).$v[‘savename‘]; $this->score_file_name = strrpos($v[‘savename‘], ‘.‘) ? substr($v[‘savename‘],0,strrpos($v[‘savename‘], ‘.‘)) : $v[‘savename‘]; $this->scorce_file_ext = $v[‘extension‘]; $this->scorce_file_size = $v[‘size‘]; $this->scorce_attch_id = $v[‘attid‘] ? $v[‘attid‘] : 0; $this->scorce_save_path = substr($v[‘savepath‘],1).$v[‘savename‘]; $this->execFile(); } } } // 检测是否满足条件转换 private function check() { $err = 0; $this->exec_error = ‘‘; $err_tip = array( 1 => ‘FlashPaper只能运行与Windows环境下‘, 2 => ‘FlashPrinter.exe转换文件不存在‘, 3 => ‘没有exec函数执行权限‘, 4 => ‘FlashPrinter(‘.$this->flashpaper_url.‘)目录不可读‘, 5 => ‘上传文件(‘.$this->scorce_file_path.‘)不存在‘, 6 => ‘保存(‘.$this->swf_savepath.‘)目录不存在或不可写‘, 7 => ‘不支持该文件格式(‘.$this->scorce_file_ext.‘)的转换‘, ); // 是否WINDOWS环境 if(!IS_WIN){ $err = 1; } // 检测FlashPaper是否存在 else if (!file_exists($this->flashpaper_url)){ $err = 2; } // 检测exec是否可以执行 else if (!function_exists(‘exec‘)){ $err = 3; } // FlashPaper是否可读 else if (!is_readable($this->flashpaper_url)){ $err = 4; } // 源文件是否可读 else if (!is_readable($this->scorce_file_path)){ $err = 5; } // 保存目录是否可写 else if (is_dir($this->swf_file_path)){ $err = 6; } // 检测文件格式 else if (!in_array($this->scorce_file_ext, explode(‘|‘, $this->allow_file_ext))){ $err = 7; } if ($err){ $this->exec_error = $err_tip[$err]; return false; } return true; } // 转换上传文件 private function execFile() { if ($this->check()) { $swf_path = $this->swf_savepath.$this->score_file_name.‘.swf ‘; // SWF文件相对路径 $this->swf_file_path = $this->swf_file_path.‘/‘.$this->score_file_name.‘.swf‘; // 转换SWF命令 $command = $this->flashpaper_url.‘ -o ‘.$swf_path.$this->scorce_file_path; $command = str_replace("/","\\",$command); // 纪录转换时间 $this->exec_start_time = $this->exectime(); // 调用FlashPaper执行转换 exec($command); $this->exec_end_time = $this->exectime(); } // 保存执行信息 $this->execSaveInfo(); } // 记录转换运行数据 private function execSaveInfo() { $state = 0; // 转换状态,默认未转换 $time = time(); $runtime = round($this->exec_end_time - $this->exec_start_time, 6); $scorcepath = $this->scorce_file_path; $swfread = M(‘swfread‘); if ($this->exec_error == ‘‘) { $sql = "INSERT INTO `log_swfread` (attid,filename,filesize,filetype,`time`,runtime,`state`,filepath,scorcepath) VALUES (‘{$this->scorce_attch_id}‘, ‘".$this->score_file_name.‘.‘.$this->scorce_file_ext."‘, ‘{$this->scorce_file_size}‘, ‘{$this->scorce_file_ext}‘, ‘{$time}‘, ‘{$runtime}‘, ‘{$state}‘, ‘{$this->swf_file_path}‘, ‘{$this->scorce_save_path}‘ );"; } else { // 转换失败 $sql = "INSERT INTO `log_swfread` (attid,filename,filesize,filetype,`time`,runtime,`state`,filepath,scorcepath,`error`) VALUES (‘{$this->scorce_attch_id}‘, ‘".$this->score_file_name.‘.‘.$this->scorce_file_ext."‘, ‘{$this->scorce_file_size}‘, ‘{$this->scorce_file_ext}‘, ‘{$time}‘, ‘{$runtime}‘, ‘0‘, ‘{$this->swf_file_path}‘, ‘{$this->scorce_save_path}‘, ‘{$this->exec_error}‘ );"; } $swfread->execute($sql); $swfid = $swfread->query("SELECT MAX(swfid) as id FROM `log_swfread`"); // socket模式:请求监听生成状态和执行时间 if ($this->socket && $this->exec_error == ‘‘){ $i = 1; while(true){ if (file_exists(‘.‘.$this->swf_file_path)){ $this->exec_end_time = $this->exectime(); $runtime = round($this->exec_end_time - $this->exec_start_time, 6); $sql = "UPDATE log_swfread SET `state` = 1 , runtime = ‘{$runtime}‘ WHERE swfid = ‘{$swfid[0][‘id‘]}‘"; $swfread->query($sql); break; } if (++$i > $this->time_limit-100) break; sleep(1); } } } // 执行时间 private function exectime() { $time = explode(" ", microtime()); $usec = (double)$time[0]; $sec = (double)$time[1]; return $sec + $usec; } } ?>
标签:des ar io os 使用 sp for on 文件
原文地址:http://my.oschina.net/u/1777508/blog/357431