Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中下载文件_Php_Json_Extjs_Http Headers - Fatal编程技术网

如何在php中下载文件

如何在php中下载文件,php,json,extjs,http-headers,Php,Json,Extjs,Http Headers,我在php中有这个函数用于下载一个文件,该文件将用数据加密,然后被下载 类:Connect只是到数据库的连接 class License extends Connect { function __construct() { parent::__construct(); } public function getLicense($vars){ // file_put_contents( "/var/log/datapost/clie

我在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请求(点击按钮)


这里可以这样做:

使用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);
使用
文件内容()

$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);
自PHP 5.1.0以来,支持通过将流句柄作为
$data
参数传递来逐段写入:

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

如果数据[这是第二个参数]是流资源,则该流的剩余缓冲区将复制到指定文件。这与使用类似

问题的另一部分:

$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);
由于您试图将数组转换为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数组


实时代码:

这里可以这样做:

使用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);
使用
文件内容()

$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);
自PHP 5.1.0以来,支持通过将流句柄作为
$data
参数传递来逐段写入:

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

如果数据[这是第二个参数]是流资源,则该流的剩余缓冲区将复制到指定文件。这与使用类似

问题的另一部分:

$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);
由于您试图将数组转换为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数组



实时代码:

您是否验证它实际上进入了if块?在其中放置一个var_dump或使用调试器逐步处理代码。是的,我看到了来自请求的响应,“echo$crypttext”网络请求在那里,但没有下载文件。是的,请验证请求是否确实触发了if块输出头。您感兴趣的是响应,而不是请求。是的,我的意思是,实际的加密文本响应已被查看。您是否验证它是否真的进入if块?在其中放置一个var_dump或使用调试器逐步处理代码。是的,我看到了来自请求的响应,“echo$crypttext”网络请求在那里,但没有下载文件。是的,请验证请求是否确实触发了if块输出头。您感兴趣的是响应,而不是请求。是的,我的意思是,实际的加密文本响应是seenI可能误解了OP,但我认为问题不是编写文件,而是强制文件下载对话。我更改了文件内容,但没有更改,我会继续努力找出问题,OK,所以我把文件和文件的内容放在一个可写的文件夹中,它工作了,但是它没有直接下载文件,我使用了javascript中的window.open,但错误表示找不到文件,我输出的文件在错误日志目录中,只是现在添加了文件内容(“/path/to/log/{$filename},$crypttext,fopen({$filename},'r');下面是echo$crypttext如何下载刚刚添加的readfile(/path/to/log/$filename);未成功我可能误解了OP,但我认为问题不在于写入文件,而在于强制文件下载对话。我对文件内容进行了更改,但没有更改,我将继续尝试解决问题OK,因此我将文件和内容放在可写文件夹中,它工作了,但它没有下载文件紧接着,我在javascript中使用了window.open,但错误是说找不到文件,我输出的文件在错误日志目录中,只是为了现在添加的文件内容(“/path/to/log/{$filename}”,$crypttext,fopen({$filename},'r'));下面是echo$crypttext我如何下载刚刚添加的readfile(/path/to/log/$filename));没有成功