Codeigniter - 重定向和下载文件

时间:2014-11-11 07:58:49

标签: php codeigniter

我想在提交(使用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)

3 个答案:

答案 0 :(得分:0)

您可以使用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

答案 1 :(得分:0)

尝试使用

redirect($filename);

如果不起作用

在你的codeignaiter index.php文件中添加以下在根文件夹中找到add i solve 无法修改标头信息 - 已由错误发送的标头并将其重定向

ob_start();

答案 2 :(得分:0)

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

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

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

OR

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;
}