0

我下载了一个文件,但它给出了无效的文件作为回报。

这是我的download_content.php

<?php    
  $filename = $_GET["filename"]; 


    $buffer = file_get_contents($filename);

    /* Force download dialog... */
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    /* Don't allow caching... */
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    /* Set data type, size and filename */
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . strlen($buffer));
    header("Content-Disposition: attachment; filename=$filename");

    /* Send our file... */
   echo $buffer; 
?> 

下载文件链接:

<a href="download_content.php?filename=/gallery/downloads/poster/large/'.$r['file'].'"> Download</a>

$r['file']包含要下载的文件名。

包含该文件的文件夹的完整路径是:

localhost/ja/gallery/downloads/poster/large/'.$r['file'].'

ja是中的根文件夹htdocs

我不知道实际问题是什么,有人可以帮助我吗?

4

3 回答 3

1

正如在另一个问题中所说,这种方式看起来更好:

$filename = $_GET["filename"];
// Validate the filename (You so don't want people to be able to download
// EVERYTHING from your site...)

// For example let's say that you hold all your files in a "download" directory
// in your website root, with an .htaccess to deny direct download of files.
// Then:

$filename = './download' . ($basename = basename($filename));

if (!file_exists($filename))
{
    header('HTTP/1.0 404 Not Found');
    die();
}
// A check of filemtime and IMS/304 management would be good here
// Google 'If-Modified-Since', 'If-None-Match', 'ETag' with 'PHP'

// Be sure to disable buffer management if needed
while (ob_get_level()) {
   ob_end_clean();
}

Header('Content-Type: application/download');
Header("Content-Disposition: attachment; filename=\"{$basename}\"");
header('Content-Transfer-Encoding: binary'); // Not really needed
Header('Content-Length: ' . filesize($filename));
Header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

readfile($filename);

也就是说,“无效文件”是什么意思?长度不好?零长度?文件名不好?MIME 类型错误?文件内容错误?眼下的一切,你的意思可能很清楚,但从我们的角度来看,这远非显而易见。

更新:显然找不到文件,这意味着filename=PHP 脚本的参数错误(指的是不存在的文件)。修改了上面的代码以允许一个目录包含所有文件,并从那里下载。

于 2012-09-25T12:05:16.110 回答
1
    <?php
    header( "Content-Type: application/vnd.ms-excel" );
    header( "Content-disposition: attachment; filename=spreadsheet.xls" );

    // print your data here. note the following:
    // - cells/columns are separated by tabs ("\t")
    // - rows are separated by newlines ("\n")

    // for example:
    echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
    echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
   ?>
于 2014-09-30T07:40:03.993 回答
0

您的 $filename 变量包含整个路径,如下所示

 header("Content-Disposition: attachment; filename=$filename");

这样做

$newfilename = explode("/",$filename);
$newfilename = $newfilename[count($newfilename)-1];

$fsize = filesize($filename);

Then pass new variable into header

header("Content-Disposition: attachment; filename=".$newfilename);
header("Content-length: $fsize");

//newline added as below
ob_clean();
flush();
readfile($filename);
于 2012-09-22T08:48:06.877 回答