1

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

1 Answers1

0

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

Umair Shah
  • 2,305
  • 2
  • 25
  • 50
  • I might be misunderstanding the OP, but I think the issue is not writing the file but forcing the file download dialogue. – Gordon Apr 26 '16 at 07:20
  • I made the file_put_content changes but no change, i will keep trying to figure the problem – Andrew Barker Apr 26 '16 at 07:37
  • Ok so I put the file the file with the contents into a writable folder and it worked, however It didnt download the file straight after, I used window.open in javascript but the error said file not found, the file I outputted is in error log directory, just for now – Andrew Barker Apr 26 '16 at 07:54
  • added file_put_contents("/path/to/log/{$filename}", $crypttext, fopen("http://example.com/path/to/log/{$filename}", 'r' )); below echo $crypttext – Andrew Barker Apr 26 '16 at 07:55
  • how do I download that just added readfile(/path/to/log/$filename); not successfull – Andrew Barker Apr 26 '16 at 08:10
  • @AndrewBarker : I am gonna have to take a look over your code besides just make sure that you share your whole code in your question so that I don't have any problem in checking your code live..! – Umair Shah Apr 26 '16 at 08:26
  • does it make sense, as you see I did change a few things since last post, those being the file_put_contents and fopen – Andrew Barker Apr 26 '16 at 08:54
  • still cant get the file to download – Andrew Barker Apr 28 '16 at 09:31
  • @AndrewBarker : Have you tried with simple function without encrypting etc and all those other things..??? – Umair Shah Apr 28 '16 at 10:11
  • @AndrewBarker : Also I am not getting that exactly what are you really trying to do? Can you properly explain with some example that what are you trying to do so may be I can then try to rewrite full code for you..! – Umair Shah Apr 28 '16 at 10:12
  • So I havea grid in sencha, this grid has records next it I have a button that has the ajax request above linked to it, this ajax request is linked to the php function, now when I click the button the selectedRecord should be downloaded, the download needs to be downloaded in the php – Andrew Barker Apr 28 '16 at 13:04
  • when i say the record will be downloaded I mean all the information from the sql query will be formatted into json and put into file and downloaded, – Andrew Barker Apr 28 '16 at 13:06
  • Simply you want to select all from your MySQL table and then encode the array of data into json and put that json data into a text file right??? – Umair Shah Apr 28 '16 at 13:16
  • @AndrewBarker : I updated the answer take a look at the answer now and let me know if it solves your problem and then you should mark the answer and consider this question solved..! – Umair Shah Apr 29 '16 at 11:51