【问题标题】:php creating a file from string with AJAXphp 使用 AJAX 从字符串创建文件
【发布时间】:2020-01-18 23:30:28
【问题描述】:
$data = base64_encode('Hello World');

            // echo '<pre>';
            // print_r(strlen(base64_decode($data)));
            // echo '</pre>';
            // exit;
            //strlen(base64_decode($data));
            //filepath
            ob_clean();
            //header("Cache-Control: no-store, no-cache");  
            header("Content-type: text/plain"); 
            header("Content-length: ".strlen(base64_decode($data)).""); 
            header("Content-Disposition: attachment; filename=qwe.Lot"); 
            exit(base64_decode($data));

我想在上面的代码中从 PHP 中的字符串创建一个文件,我将它作为我的控制器。它只打印字符串而不下载文件。

  1. 如何在 Code Igniter 中从字符串创建文本文件?

更新 这是浏览器调试中的输出

【问题讨论】:

  • 尝试使用 echo 而不是退出? (不确定这是否可行)
  • 还有 Content-TypeContent-Length 标题的大小写。
  • 我使用了echo (base64_decode($data)); 相同的结果,只是将字符串放在浏览器中而不是下载@ErtySeidohl
  • @DhavalPurohit 我不明白你的意思
  • 标题中的tl 应该大写@gurradio

标签: php ajax string file codeigniter


【解决方案1】:

这种方式可能会有所帮助。

<?php
$file = 'file.txt';
$data = 'this is your string to write';
file_put_contents($file, $data);
?>

【讨论】:

    【解决方案2】:

    用于文件保存

    $data = base64_encode('Hello World');
    $filename = "text.txt";
    file_put_contents(filename, data);
    

    用于文件查看

    $filename = "text.txt";
    $content = json_decode(file_get_contents($filename));
    

    此文件将保存在您的项目根文件夹中。您可以将文件路径放在文件名之前。

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2012-05-26
      • 2017-06-19
      • 1970-01-01
      相关资源
      最近更新 更多