您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Flash Paper 仿百度文库的功能实现

2025/5/4 0:42:49发布17次查看
具体是哪位大神发给我的,我就忘了,拿出来分享下吧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).'
';
        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;
    }
}
?> ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product