尝试在核心php中下载文件并获取无效文件作为响应 [英] try to download file and getting invalid file in response in core php

查看:83
本文介绍了尝试在核心php中下载文件并获取无效文件作为响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了文件,但返回的文件无效。

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

这是我的 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'] 包含

包含文件的文件夹的完整路径为:

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

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

ja htdocs 中的根文件夹。

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

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

推荐答案

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

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

也就是说,无效文件是什么意思?长度不好?零长度?文件名错误? MIME类型错误?文件内容错误?眼下所有事物对您来说含义都很清楚,但从我们的角度来看,它远非显而易见。

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

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.

这篇关于尝试在核心php中下载文件并获取无效文件作为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆