下载根目录以外的文件 [英] Downloading files outside the webroot

查看:149
本文介绍了下载根目录以外的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,因为我知道这是之前已经涵盖了许多次的话题首先歉意 - 相信我,我知道,我读过所有的previous问题和答案,但仍无法得到这个的工作。

OK, firstly apologies as I realise that this is a topic which has been covered many times before - believe me I know, I've read all of the previous questions and answers and still can't get this to work.

我有一个包含下载文件的文件夹。为了安全起见,我所在的根目录以外的文件。然而,尽管我尽了最大努力,我不能让我的PHP脚本下载该文件。

I have a folder containing downloadable files. For security purposes I've located this file outside the webroot. However, despite my best efforts, I can't get my php script to download the file.

我使用的是Linux VPS Apache服务器使用的Plesk 11。

I'm using a Linux VPS Apache Server using Plesk 11.

的(简化的)的文件结构如下。在的httpdocs 文件夹是根目录。在私人/ UPLOADEDFILES 文件夹是我想从下载。

The (simplified) file structure is as follows. The httpdocs folder is the webroot. The private/uploadedfiles folder is where I want to download from.

-var
 - www
  - vhosts
   - mydomain.org.uk
    - httpdocs (webroot)
    - private
     - uploadedfiles

我使用jQuery的Ajax调用的文件名传递给被称为PHP脚本 downloadscript.php 。该脚本位于中的的httpdocs 根目录。该脚本如下:

I'm using a jQuery ajax call to pass the filename to a PHP script called downloadscript.php. This script sits within the httpdocs webroot. The script is as follows:

<?php

$filename = $_POST['fbpath'];
$path = '/var/www/vhosts/mydomain.org.uk/private/uploadedfiles/' . $filename;

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;

?>

AJAX调用工作没有问题,但我让我的PHP记录以下错误消息:

The ajax call is working with no issues, but I'm getting the following error message on my PHP logs:

 PHP Warning: readfile(/var/www/vhosts/mydomain.org.uk/private/uploadedfiles/filename.docx): failed to open stream: No such file or directory

我已经检查,双重检查和三重检查和文件中的 UPLOADEDFILES 文件夹内肯定存在的。

我也检查了它不是一个的open_basedir 限制的问题 - 我是pretty确保它不是

I have also checked that it isn't an open_basedir restriction issue - I'm pretty sure it isn't.

我敢肯定有很简单的东西我失踪? - 我要去哪里错了

I'm sure there's something really simple I'm missing - where am I going wrong?

作为附加额外的,我没有写剧本尚未对上传文件 - ?有什么我应该提前知道之前,这个走在前面

As an additional extra, I haven't written the script for uploading files yet - is there anything I should know in advance before going ahead with this?

谢谢!

推荐答案

多的试验和错误之后,我似乎已经解决了这个问题。

After much trial and error, I appear to have solved the problem.

问题是在使用jQuery /阿贾克斯。

The issue was in using jQuery/Ajax.

当我改变了 downloadscript.php 文件被访问,从链接直接 $ _ GET 请求的方式在页面上,它的工作一种享受。

When I changed the way the downloadscript.php file is accessed to a direct $_GET request from the link on the page, it worked a treat.

无论如何,感谢您的帮助大家!

Anyway, thanks for your help everyone!

克里斯

这篇关于下载根目录以外的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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