繁体   English   中英

如何在php中下载文件

[英]How to download a file in php

我在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 );
        }
    });
},

在这里它可以这样做:

通过使用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()支持通过将stream-handle作为$data参数传递来逐个编写:

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

从手册:

如果数据 [即第二个参数]是流资源,则该流的剩余缓冲区将被复制到指定的文件。 这与使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM