本人写了个文件下载程序,点下载时不弹出另存框,而是将文件里的内容输出到页面上了?请问这是怎么回事。我想要弹出另存框的。
html代码:
<a href="download_dispose.php?sourceCodeID=<?php echo $row[sourceCodeID]; ?>"><img alt="下载" src="Images/download.gif" border="0px" /></a>php代码:
<?php
    include("conn/sql_conn.php");
    $sourceCodeID=$_GET[sourceCodeID];
    $sql=mysql_query("select A.sourceCodePath,B.sourceCodeTypeName from sourcecodes A left join sourcecodetype B on A.sctID=B.sourceCodeTypeID where A.sourceCodeID='$sourceCodeID' order by A.sourceCodeID DESC limit 0,1",$conn);
    $info=mysql_fetch_array($sql);
    if($info==false)
    {
 echo "暂无源码!";
    }
    else
    {
     echo "源码信息已找到!<br />";
     $sourceCodePath="admin/".$info[sourceCodePath];
     $filename=basename($sourceCodePath);
     if (!file_exists($sourceCodePath))
 { //检查文件是否存在
  echo "文件未找到";
 }
 else
 {
  $file=fopen($sourceCodePath,"r");
  header("Content-type:application/octet-stream");
  header("Accept-ranges:bytes");
  header("Accept-length:".filesize($sourceCodePath));
  header("Content-Disposition:attachment;filename=".$filename);
  echo fread($file,filesize($sourceCodePath));
      fclose($file);
      exit;
 }
}
?>

解决方案 »

  1.   

    参考这个http://topic.csdn.net/u/20110411/15/a3ed9fd9-a098-4897-85be-1d92bdc72247.html
      

  2.   

    输出到页面上了,那就是header没对,仔细检查一下
      

  3.   

    header("Content-type:application/octet-stream");改成:header("Content-Type: application/force-download");
      

  4.   

    打开报错,error_reporting(E_ALL), 看看是否有输出错误提示另外保证header设置正确,
    <?php 
    ob_start();
    // 省略其他代码
    header("Content-Disposition:attachment;filename=".$filename);
    ob_end_flush();
    echo fread($file,filesize($sourceCodePath));
      fclose($file);
      exit;