Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 强制下载文件不起作用_Php_Html_Download - Fatal编程技术网

Php 强制下载文件不起作用

Php 强制下载文件不起作用,php,html,download,Php,Html,Download,当用户单击链接时,我正在尝试下载该文件。该文件位于其他网站上,我正在传递该文件的url,例如-xyz.com/filename.mp3 echo "<a href=/direct_download.php?file=$link[$j]><img border='0' src='images/dl.gif' ></a>"; echo”“; 这是我的下载文件脚本 <?php $file_name = $_GET['file']; //$files =

当用户单击链接时,我正在尝试下载该文件。该文件位于其他网站上,我正在传递该文件的url,例如-xyz.com/filename.mp3

echo "<a href=/direct_download.php?file=$link[$j]><img border='0' src='images/dl.gif' ></a>";
echo”“;
这是我的下载文件脚本

<?php

$file_name = $_GET['file'];
//$files = rawurldecode($file_name);
echo $file_name;


if(is_file($file_name)) {
echo $file_name;

    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }


    switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
    case 'mp3': $mime = 'audio/mpeg';break;
        default: $mime = 'application/force-download';
    }
    header('Pragma: public');   
    header('Expires: 0');       
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file_name));    
    header('Connection: close');
    readfile($file_name);       
    exit();

}
?>

更改
内容类型
,如下所示:

header('Content-Type: application/octet-stream');
编辑:尽管我仍然使用
application/octet-stream
作为内容类型,但请尝试一下:

<?php
/**
 * Make sure that the VERY FIRST THING IN THE DOCUMENT IS <?php
 * There can't be any spaces or anything else before it, because if
 * there is then header information has already started to be sent
 * back.
 */

/**
 * Avoid warnings, notices, etc by check $_GET's value first:
 */
$file_name = ($_GET && isset($_GET['file'])) ? $_GET['file'] : FALSE;
//$files = rawurldecode($file_name);

/**
 * As soon as you "echo" something, headers are sent back to the
 * browser, the same way adding space(s) before the opening <?php
 * tag will. Once headers are sent, they can't be modified or you
 * will receive an error.
 */
//echo $file_name;


if(is_file($file_name)) {
  /**
   * See above for info on headers.
   */
  //echo $file_name;

  if (ini_get('zlib.output_compression')) {
    ini_set('zlib.output_compression', 'Off');
  }
  switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
    case 'pdf': $mime = 'application/pdf'; break;
    case 'zip': $mime = 'application/zip'; break;
    case 'jpeg':
    case 'jpg': $mime = 'image/jpg'; break;
    case 'mp3': $mime = 'audio/mpeg';break;
    /**
     * Although this works sometimes, I've had better luck with just
     * setting the content type to "application/octet-stream"
     * for anything that needs to be "forced" as a download.
     */
    // default: $mime = 'application/force-download';
    default: $mime = 'application/octet-stream';
  }
  header('Pragma: public');   
  header('Expires: 0');       
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime($file_name)).' GMT');
  header('Cache-Control: private',false);
  header('Content-Type: '.$mime);
  header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: '.filesize($file_name));    
  header('Connection: close');
  readfile($file_name);       
  exit();
}
?>

我猜您没有包括目录

$file_name = $_GET['file'];

$directory = 'set me up'; // directory for files

if(is_file($directory.$file_name)) {

$file_parts = pathinfo($directory.$file_name);

    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }


    switch($file_parts['extension']) {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
    case 'mp3': $mime = 'audio/mpeg';break;
        default: $mime = 'application/force-download';
    }

$size = filesize($directory.$filename) ;
header("Content-Type: application/force-download; name=\"". $file_parts['basename'] ."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size ."");
header("Content-Disposition: attachment; filename=\"". $file_parts['basename'] ."\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo (readfile($directory.$file_name));
} 

注释掉echo$file\u name;为了避免在进行标题声明之前将标题发送到浏览器,只需从browserGood调用@KaiQing中删除文本,就不会发生任何事情。我刚才还注意到它在读取文件后退出。。。我以前从未做过,但我想知道,嗯。。。现在我得检查一下。(好奇)我什么都试过了,但没用。请从网络上获取任何mp3链接,并告诉我这将如何工作。
$file\u name
不完全是somesong.mp3。它是整个路径,包括目录,如somewebsite/music/song.mp3。为什么你们不找一个文件的链接,自己试试。在url中包含完整路径不是一个好主意,因为它会让你们遭受黑客攻击。链接在哪里?顺便说一下。请给出一个链接的确切示例。您需要的是相对路径而不是绝对路径。@mjosh:请查看以上编辑,并确保在打开
之前不存在空格或任何其他内容。您为什么不在某个链接上尝试此代码,并告诉我它是否对您有效?我已经尝试过了,而且效果很好。我不会在我的公共服务器上发布它,但是。。。这当然不是针对个人的(所以请不要误解这一点),但我不会公开使用这段代码的原因是因为代码本身没有经过消毒,因此在我看来是不安全的。但它在我的本地计算机上确实可以工作。另外值得一提的是,在
echo''中,链接位置(href)周围没有任何引号--事实上,您是否尝试过直接访问链接(而不是单击链接)?这很可能是一个促成因素。。。将此添加到解决方案中。为了进一步解释,我尝试直接访问链接,但它在我的浏览器quicktime player中打开。
echo '<a href="/direct_download.php?file=' . $link[$j] . '"><img border="0" src="images/dl.gif" /></a>';
$file_name = $_GET['file'];

$directory = 'set me up'; // directory for files

if(is_file($directory.$file_name)) {

$file_parts = pathinfo($directory.$file_name);

    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }


    switch($file_parts['extension']) {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
    case 'mp3': $mime = 'audio/mpeg';break;
        default: $mime = 'application/force-download';
    }

$size = filesize($directory.$filename) ;
header("Content-Type: application/force-download; name=\"". $file_parts['basename'] ."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". $size ."");
header("Content-Disposition: attachment; filename=\"". $file_parts['basename'] ."\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo (readfile($directory.$file_name));
}