php 异步形式调取导出数据

php部分

ajax请求此部分
function aysncexec()
    {

        $lock_file = 'filelock.lock';

        if (file_exists($lock_file)) {
            exit(json_encode(array('code' => 0)));
        }

        $url = base_url() . 'execcmd';


        $this->_curl($url, [], 1);

        exit(json_encode(array('code' => 1)));
    }


_curl($url, $data = null, $timeout = 1)
    {


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        if ($timeout > 0) { //超时时间秒
            curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        }
        $output = curl_exec($curl);
        $error = curl_errno($curl);
        curl_close($curl);
        if ($error) {
            return false;
        }
        return $output;
    }


curl执行此部分
function execcmd($keys = "")
    {




        $datas = $this->input->post(null, true);




        if (!empty($datas)) {
            $search_file = 'search.json';

            unlink($search_file);

            file_put_contents('search.json', json_encode($datas, JSON_UNESCAPED_UNICODE));
        }


        $php_path = '/opt/modules/php/bin/php';
        ignore_user_abort(true); // 忽略客户端断开 
        set_time_limit(0);       // 设置执行不超时
        $index_path = APPPATH . 'index.php';
        $clis = $php_path . " " . $index_path . '2>&1';
        exec($clis, $output);


    }

下载

function downloads()
    {
        $file_path ='data/e.xlsx';
        $lock_file =  'data/filelock.lock';
        $search_file ='data/search.json';

        $filesize = filesize($file_path);

        header('Content-Description:File Transfer');
        header('Content-Type:application/octet-stream');
        header('Content-Transfer-Encoding:binary');
        header('Accept-Ranges: bytes');
        header('Expires:0');
        header('Cache-Control:must-revalidate');
        header('Pragma:public');
        header('Content-Length:' . $filesize);
        header('Content-Disposition:attachment;filename=tests.xlsx');
        $fp = fopen($file_path, 'rb');
        fseek($fp, 0);
        while (!feof($fp)) {
            echo fread($fp, 1024 * 8); //输出文件
            flush(); //输出缓冲
            ob_flush();
        }
        fclose($fp);
        unlink($file_path);
        unlink($lock_file);
        unlink($search_file);
        exit();
    }

html部分

   $.ajax({
                  method: "POST",
                  url: "aysncexec",
                  data: data_search,
                  dataType: "json",
                  timeout: 5000 
                }).success(function(res){
                  layer.close(index);
                  if (res.code=="0") {
                    alert("正在运行");
                    return;
                  } else {
                    
                    window.location.reload();
                  }
                  
                }).done(function(data) {
                
                    console.log(data.fridge);
                }).fail(function(jqXHR, status) {
                    
                    console.log(status);
                    
                });

遇见的问题:nginx 499 错误
php 主动断开 查看php目录 权限不对

posted @ 2023-05-12 16:33  尘梦  阅读(109)  评论(0编辑  收藏  举报