Flash Paper 仿百度文庫的功能實現
/*
Description: FlashPaper文檔轉換,轉換可打印的文檔爲FLASH格式並保存轉換的相關信息到log_swfread表
http://www.thinkphp.cn/code/5.html
* 使用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;
}
}
?>
Description: FlashPaper文檔轉換,轉換可打印的文檔爲FLASH格式並保存轉換的相關信息到log_swfread表
http://www.thinkphp.cn/code/5.html
* 使用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;
}
}
?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.