try to download file and getting invalid file in response in core php

≡放荡痞女 提交于 2019-12-20 06:29:44

问题


I download a file but it gives invalid file in return.

Here's my 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; 
?> 

download file link:

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

$r['file'] contains the file name to be downloaded.

The complete path of the folder which contain the file is:

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

ja is the root folder in htdocs.

I don't know what the actual problem is, can anyone help me out please?


回答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";
   ?>



回答2:


As said in the other question, this way looks better:

$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);

That said, what does "invalid file" mean? Bad length? Zero length? Bad file name? Wrong MIME type? Wrong file contents? The meaning may be clear to you with everything under your eyes, but from our end it's far from obvious.

UPDATE: apparently the file is not found, which means that the filename= parameter to the PHP script is wrong (refers a file that's not there). Modified the code above to allow a directory to contain all files, and downloading from there.




回答3:


Your $filename variable contains whole path as below

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

Do like this

$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);


来源:https://stackoverflow.com/questions/12542203/try-to-download-file-and-getting-invalid-file-in-response-in-core-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!