PHP损坏的文件

时间:2015-06-19 08:46:47

标签: php easyphp

这是我点击下载按钮的页面。

这是我在网上找到的但是没有用的东西

why my downloaded file is alwayes damaged or corrupted?

Force Downloading a PDF file, corrupt file     

if(isset($_GET['id'])){
 $id    = $_GET['id'];

 $con = mysqli_connect("localhost:3306", "waduser", "waduser", "fyp");
if(!$con) {
  die("cannot connect: " . mysqli_error());
 }
 mysqli_select_db($con,"fyp");

 $query = "SELECT * FROM docu where id ='" .$id ."'";
 $result = mysqli_query($con,$query) or die('Error, query failed');
 if($row =  $result ->  fetch_array(MYSQLI_ASSOC))
 {
    $name = $row['name'];
    $type = $row['type'];
    $content = $row['content']; //content of file
    $size = $row['size']; //file size
    header('Content-Type:"' . $type . '"');
    header('Content-length:' . $size  .'');
    header('Content-Disposition: attachment; filename="' .$name. '"');

  } 
 }
?>

这是下载代码。当我下载文件时,它说它已损坏。

var data = "<p>This is new tab'</p><br/>"+message_entered;
newWindow = window.open("data:text/html," + encodeURIComponent(data),
                   "_blank", "width=200,height=100");
newWindow.focus();

1 个答案:

答案 0 :(得分:0)

我认为Jim试图说的是你已经发送了所有标题,但你实际上并没有发送文件。

所以试试

$name = $row['name'];
$type = $row['type'];
$content = $row['content']; //content of file
$size = $row['size']; //file size
header('Content-Type:"' . $type . '"');
header('Content-length:' . $size  .'');
header('Content-Disposition: attachment; filename="' .$name. '"');
echo $content;

另外我认为至少在href属性中删除short_tag用法周围的空格会更安全。

<p><a href="download.php?id=<?=$id?>">Name : <?= $name ?></a></p>