【问题标题】:Codeigniter - redirect and download fileCodeigniter - 重定向和下载文件
【发布时间】:2014-11-11 07:58:48
【问题描述】:

我想在提交后强制下载(使用 PhpWord 生成的 .docx 文件),同时重定向到另一个页面。

这里是生成 DOCX 文件的代码:

$objWriter->save($filename);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
header('Location: http://localhost/contracto_0.4/index.php/contrac)
readfile($filename);
unlink($filename); // deletes the temporary file

在此之后,我尝试加载一个视图,我收到了来自 PHP 的这条消息:

Cannot modify header information - headers already sent by (output started at C:\wamp\www\contracto_0.5\system\core\Exceptions.php:185)

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    你可以使用download_helper:

        if ( ! function_exists('force_download'))
    {
     function force_download($filename = '', $data = '')
     {
      if ($filename == '' OR $data == '')
      {
       return FALSE;
      }
    
      // Try to determine if the filename includes a file extension.
      // We need it in order to set the MIME type
      if (FALSE === strpos($filename, '.'))
      {
       return FALSE;
      }
    
      // Grab the file extension
      $x = explode('.', $filename);
      $extension = end($x);
    
      // Load the mime types
      @include(APPPATH.'config/mimes'.EXT);
    
      // Set a default mime if we can't find it
      if ( ! isset($mimes[$extension]))
      {
       $mime = 'application/octet-stream';
      }
      else
      {
       $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
      }
    
      // Generate the server headers
      if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
      {
       header('Content-Type: "'.$mime.'"');
       header('Content-Disposition: attachment; filename="'.$filename.'"');
       header('Expires: 0');
       header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
       header("Content-Transfer-Encoding: binary");
       header('Pragma: public');
       header("Content-Length: ".strlen($data));
      }
      else
      {
       header('Content-Type: "'.$mime.'"');
       header('Content-Disposition: attachment; filename="'.$filename.'"');
       header("Content-Transfer-Encoding: binary");
       header('Expires: 0');
       header('Pragma: no-cache');
       header("Content-Length: ".strlen($data));
      }
    
      exit($data);
     }
    }
    

    在你的控制器中:

    function donwload(){
    $this->load->helper('download');
    $data = file_get_contents('path/file.docx');
    force_download('file.docx', $data);
    } 
    

    以上代码在ellislab.com中可用

    【讨论】:

      【解决方案2】:

      可能是因为你有这两行代码,请更改这两行

      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename='.$filename);
      

      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename="' . $filename . '"');
      

      header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
      header('Content-Disposition: attachment; filename="' . $filename . '"');
      

      也许我不必逐行解释,因为它真的很容易理解。请参考下面我的旧代码作为指南。实际上它仍在我的现场网站上运行。随时测试和修改它。

      $error = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
      
      if(isset($_GET['file']) && basename($_GET['file']) == $_GET['file'])
      {
          $filename = $_GET['file'];
      }
      else
      {
          $filename = null;
      }
      
      if($filename != null)
      {
          $path = '../files/' . $filename;
          if (file_exists($path) && is_readable($path))
          {
              header('Pragma: public');
              header('Expires: 0');
              header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
              header('Content-Disposition: attachment; filename="' . $filename . '"');
              header('Content-Length: ' . filesize($path));
              header('Content-Transfer-Encoding: binary');
      
              $file = @fopen($path, 'rb');
              if($file)
              {
                  fpassthru($file);
                  exit;
              }
              else
              {
                  echo $error;
              }
          }
          else
          {
              echo $error;
          }
      }
      else
      {
          echo $error;
      }
      

      【讨论】:

        【解决方案3】:

        尝试使用

        redirect($filename);
        

        如果不工作

        在您的 codeignaiter index.php 文件中添加以下定位在根文件夹中添加我解决 无法修改标头信息 - 标头已被错误发送并重定向

        ob_start();
        

        【讨论】:

          猜你喜欢
          • 2011-08-30
          • 2013-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-19
          • 1970-01-01
          • 2020-05-27
          相关资源
          最近更新 更多