繁体   English   中英

PHP脚本访问否则“全部拒绝”的目录(通过.htaccess)

[英]PHP script access a directory that is otherwise 'deny all' (Via .htaccess)

我正在尝试使用php脚本列出使用.htaccess文件中的'deny all'命令保护的备份目录的内容。

这背后的原因是wordpress的备份插件仅供管理员使用,我希望让排名较低的用户能够从在线下载备份(通过简单的仪表板小部件)。

您可以在下面找到我正在使用的代码,并且由于htaccess文件而遇到403错误时它会跳闸。

//define the directory (this is where the backup plugin store the .zip files)
$directory = site_url() . "/wp-content/backups/";

//display the generated directory for testing
echo '<b>Scanning: </b>' . $directory . '</br></br>';

//display a link to a test.zip I know exists for testing
echo '<b>Manual Link: </b> <a href="' . $directory . 'test.zip" target="_blank">test.zip</a></br></br>';

//get all files in the directory with a .zip extension.
$files = glob($directory . "*.zip");

//print each file name as a download link
foreach($files as $file){
    echo '<a href="' . $file . '">' . $file . '</a> </br>';
}

您可以提供的任何帮助都将是最有帮助的。

谢谢,安迪。

代码更新,以便glob正常工作

//Define the backup folder location
$backup_location = "wp-content/backups/";

//Set the file directory for glob
$directory = $_SERVER['DOCUMENT_ROOT'] . $backup_location;

//Prints the directory for testing purposes
echo '<b>Scanning: </b>' . $directory . '</br></br>';

//Filter the discovered files
$files = glob($directory . "*.*");

//Echo the filtered files
foreach($files as $file){
    echo '<a href="' . site_url() . '/' . $backup_location . basename($file) . '">' . basename($file) . '</a> </br>';
}

您目前只是提供用户无权访问的文件的链接。 如果不添加任何权限,这将无效。

在此设置中可以使用当前页面实际向用户提供文件。

你基本上做的是让php读取文件(它可以访问该文件),向用户发送标题,然后发送内容。 使用像PHP手册中的示例代码:

http://php.net/manual/en/function.readfile.php

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

你的问题是这样的:

 $directory = site_url() . "/wp-content/backups/";

您无法通过HTTP进行目录列表。 这不起作用(无论访问限制如何)。 您需要在本地目录上运行glob()等。

 $directory = "./wp-content/backups/";

它在glob手册页面上正确说:

注意:此功能不适用于远程文件,因为必须可以通过服务器的文件系统访问要检查的文件。

暂无
暂无

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

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