如何下载 php 中的文件
How to download a file in php
我在php有这个功能可以下载文件,文件会用数据加密,然后下载
Class: 连接只是连接到数据库
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()
支持通过将流句柄作为$data
参数传递来逐段写入:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
来自手册:
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()
.
你问题的另一部分:
由于您尝试将数组转换为 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
我在php有这个功能可以下载文件,文件会用数据加密,然后下载
Class: 连接只是连接到数据库
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()
支持通过将流句柄作为$data
参数传递来逐段写入:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
来自手册:
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()
.
你问题的另一部分:
由于您尝试将数组转换为 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