【问题标题】:How to download a file in php如何在php中下载文件
【发布时间】:2016-04-26 06:44:04
【问题描述】:

我在php中有这个函数用于下载文件,文件会被数据加密,然后被下载

类:Connect 只是连接到数据库

class License extends Connect {

    function __construct() {
        parent::__construct();
    }

        public function getLicense($vars){
//        file_put_contents( "/var/log/datapost/clientinfo.log", date('r'). " ". var_export($vars,true) . PHP_EOL, FILE_APPEND );
        $sql = "select * from License where license_id={$vars->data->license_id}";
        $res = $vars->db->query( $sql );
        if($obj=$res->fetch_object()){

            $filename = "License".$vars->data->license_id.".json";

            // get the private key
            $pk = file_get_contents("/var/www/datapost/clientinfo/keys/key1.pem");
            // // private key
            $res = openssl_get_privatekey($pk,"passsword");
            // // encrypt it
            $x=openssl_private_encrypt( json_encode($obj),$crypttext,$res);

            // save the encryption
            // 

file_put_contents("/var/www/datapost/clientinfo/license/license{$vars->data->license_id}}.json",$crypttext);
//            header('Content-Description: File Transfer');
            header('Content-Type: text/plain');
            header('Content-Disposition: attachment; filename="' . $filename . '"');
//            header('Expires: 0');
//            header('Cache-Control: must-revalidate');
//            header('Pragma: public');
//            header('Content-Length: ' . strlen($crypttext));
            echo $crypttext;
            file_put_contents("/var/log/datapost/{$filename}", $crypttext, fopen("http://192.168.200.114/var/log/datapost/{$filename}", 'r' ));
            flush();
            file_put_contents( "/var/log/datapost/clientinfo.log", date('r'). " WOOHOO! $crypttext" . PHP_EOL, FILE_APPEND );
        }
        else {
            file_put_contents( "/var/log/datapost/clientinfo.log", date('r'). " AAARG SHIT ".var_export($res,true) . PHP_EOL, FILE_APPEND );
        }
    }
    }

但由于某种原因它无法正常工作,数据是在 http 请求中发送的,但没有与数据一起下载文件,感谢任何帮助

这是 extjs 应用程序中的 ajax 请求,(点击按钮)

onButtonClick7: function(button, e, eOpts) {
Ext.Ajax.request({
    url: 'system/index.php',
    method: 'POST',
    params: {
        class: 'License',
        method: 'getLicense',
        data: Ext.encode({
        license_id: Ext.getCmp('overviewGrid').selection.data.license_id
            })
        },
        success: function( response ){
            var object = Ext.decode( response.responseText, true );
            //Ext.getStore('LicenseAllStore').reload();
            //             window.open('http://192.168.200.114/datapost/clientinfo/license/BLAH_BLAGH3.json');
        },
        failure: function( response ){
            var object = Ext.decode( response.responseText, true );
            Ext.MessageBox.alert( 'Status', object.message );
        }
    });
},

【问题讨论】:

  • 你确认它实际上进入了 if 块吗?在其中放置一个 var_dump 或使用调试器单步执行代码。
  • 是的,我看到了请求的响应,'echo $crypttext'
  • 有网络请求,但没有下载文件
  • 是的,请验证请求是否真正触发了输出标头的 if 块。您对响应感兴趣,而不是请求。
  • 是的,我的意思是看到实际的加密文本响应

标签: php json extjs http-headers


【解决方案1】:

这里可以这样:

通过使用 CURL:

$ch = curl_init();
$source = "http://someurl.com/afile.zip";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "/asubfolder/afile.zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

通过使用file_put_contents()

自 PHP 5.1.0 起,file_put_contents() 支持通过将流句柄作为 $data 参数传递来逐段写入:

file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));

来自手册:

如果 data [即第二个参数] 是流资源,则该流的剩余缓冲区将被复制到指定文件。这与使用类似 stream_copy_to_stream().

您问题的另一部分:

由于您尝试将数组转换为 json,然后将该 json 代码保存到文本文件中,因此, 这是一种完全可以做到的方法:

<?php
$data = array(
    array('team_name' => "Team 1", 'wins' => "3", 'losses' => "0" ), //3
    array('team_name' => "Team 2", 'wins' => "2", 'losses' => "1" ), //2
    array('team_name' => "Team 3", 'wins' => "1", 'losses' => "2" ), //1
    array('team_name' => "Team 4", 'wins' => "0", 'losses' => "3" ), //1
    array('team_name' => "Team 5", 'wins' => "3", 'losses' => "0" ), //3
    array('team_name' => "Team 6", 'wins' => "2", 'losses' => "1" ) ); //2

//if you want to just print the array here
echo json_encode($data);

// If you want to save the array into a text file
$json_file = fopen("array_into_json.txt" , "a") or die("Unable to open file!");
fwrite($json_file,json_encode($data). "\r\n");
fclose($json_file);

?>

注意:您可以使用此代码对从数据库中获取的数组执行相同操作,只需将 $data 数组替换为您的 MySQLi 数组..!

直播代码: https://eval.in/562247

【讨论】:

  • 我可能会误解 OP,但我认为问题不在于写入文件,而是强制文件下载对话。
  • 我更改了 file_put_content 但没有更改,我会继续努力解决问题
  • 好的,所以我把文件和内容一起放到了一个可写的文件夹中,它可以工作,但是它没有立即下载文件,我在javascript中使用了window.open,但是错误说找不到文件,我输出的文件在error log目录下,暂时就这样
  • 添加了 file_put_contents("/path/to/log/{$filename}", $crypttext, fopen("example.com/path/to/log{$filename}", 'r' ));下面回声 $crypttext
  • 如何下载刚刚添加的 readfile(/path/to/log/$filename);不成功
猜你喜欢
  • 2018-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多