我在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 );
}
});
},
答案 0 :(得分:0)
这里可以这样做:
使用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'));
从手册:
如果 数据 [这是第二个参数]是一个流资源,该流的剩余缓冲区将被复制到指定的文件中。这与使用类似
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