简体   繁体   中英

How to download a file in php

I have this function in php for downloading a file, the file will be encrypted with data, and then be downloaded

Class: Connect is simply connection to database

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 );
        }
    }
    }

but for some reason it is not workiing, the data is being sent in http request but no file is being downloaded with data, any help appreciated

Here is ajax request in extjs application, (on button click)

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

Here it can be done like this way :

By USING 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);

By USING file_put_contents() :

Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:

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

From the manual:

If data [that is the second argument] is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using stream_copy_to_stream() .

Another Part Of Your Question :

Since you are trying to convert an array into json and then save that json code into a text file so, Here is an approach exactly that how you can do it :

<?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);

?>

Note : You can do the same with the array which you fetch from your database with this code just replacing the $data array with your MySQLi array..!

Live Code : https://eval.in/562247

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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