簡體   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