简体   繁体   English

使用PHP指针下载文件

[英]Downloading file using PHP pointer

I am storing files above the root directory of my server, and I'm planning on giving users a download by using a php file. 我将文件存储在服务器的根目录上方,并且正计划使用php文件为用户提供下载。

Here is my code: For the download link: 这是我的代码:对于下载链接:

<a href="'.FILEGRAB_ADR.'?adr='.$file.'">Download</a>

and for the FileGrab.php file: 对于FileGrab.php文件:

<?php
//This will grab a file from the server

if (file_exists(UPLOAD_ADR.$_GET['adr'])) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="example.CATPart"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header("Content-length: ".filesize(UPLOAD_ADR.$_GET['adr'])); 

   readfile(UPLOAD_ADR.$_GET['adr']);
   exit;
}
?>

I got the above code (in essence) from here 我从这里得到了上面的代码(本质上)

FileGrab.php is loading but it looks like it is just spewing out a raw text form of the file, arbitrary text/symbols etc. FileGrab.php正在加载,但似乎只是喷出了文件的原始文本格式,任意文本/符号等。

Checked that the file exists, and file size returns a value. 检查文件是否存在,文件大小返回一个值。 Don't know how to get anymore errors to check out of it! 不知道如何获取错误以进行检查!

Does it make a difference this is a .CATPart file, which is a little abstract but necessary... 这是一个.CATPart文件,这有点抽象,但有必要吗?

Any help greatly appreciated :) 任何帮助,不胜感激:)

Try to use buffering functions by surrounding your code between ob_start() and ob_end_flush (); 通过将代码放在ob_start()和ob_end_flush()之间来尝试使用缓冲功能; . plus of course make sure that no spaces or echos are used before sending the headers . 另外当然,在发送头文件之前,请确保不使用空格或回声。

For more info check these links: 有关更多信息,请检查以下链接:

http://php.net/manual/en/ref.outcontrol.php http://php.net/manual/zh/ref.outcontrol.php

Why use output buffering in PHP? 为什么要在PHP中使用输出缓冲?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM