PHP脚本根据用户权限动态显示目录中的文件?

问题描述:

香港专业教育学院做了以下脚本来显示从目录中的文件,如果用户在签署:PHP脚本根据用户权限动态显示目录中的文件?

<?php 
session_start(); 
require_once("path/file.php"); 

if (!empty($_SESSION[username])) 
{echo "You <b>$_SESSION[username]</b> are registered."; 

$dirPath = dir('includes/path/'); 
$docArray = array(); 
while (($file = $dirPath->read()) !== false) 
{ 
    if ((substr($file, -3)=="pdf") || (substr($file, -3)=="doc")) 
    { 
    $docArray[ ] = trim($file); 
    } 
} 
$dirPath->close(); 
sort($docArray); 
$c = count($docArray); 
foreach($docArray as $filename) 
{ 
    echo "<div><a href=\"./includes/path/$filename\">Download '$filename'</a></div>"; 
    echo "<br/>"; 
} 
include('logout.php'); 
} 

else 
{echo "somethingsomething"; 

include('login.php'); 
} 
?> 

在成员表中有两列MSV和LTP可能的值0,1,我也不得不目录/路径/ LTP和/路径/ MSV。

我需要一个脚本的补充,如果用户有权限LTP或/和MSV,文件将相应地显示。

+0

你不显示任何代码处理你的成员表(我假设它是一个MySQL数据库),并且我假设'path/MSV' /'path/LTP'部分将会是'$ dirPath'?请澄清。 – ChunkyBaconPlz

+0

是的,所有文件都存储在成员表中的mysql数据库中。用户手动插入用户名|密码|公司| LTP | MSV。 – KornholioBeavis

+0

和文件路径目前是/ includes/published /这应该被拆分成两个目录/ includes/MSV和/ includes/LTP/ – KornholioBeavis

您将需要存储到$_SESSION领域LTPMSV,并尝试像这样在PHP文件,您正在使用,以读取这些文件夹

somethig的值读取LTP文件夹

if(!empty($_SESSION[LTP])){ 
//code to read LTP folder 
}else{ 
//Cannot read LTP folder 
} 

读MSV夹

if(!empty($_SESSION[MSV])){ 
//code to read MSV folder 
}else{ 
//Cannot read MSV folder 
} 

假设0是否认ACCES和1授予读取权限

+0

这些值存储在一个mysql数据库中。所有成员都是手动插入的用户名\ – KornholioBeavis

+0

@ user1185197你知道如何从mysql获取这些值并将它们存储到$ _SESSION中? –

查询您的数据库以查找用户,并在您获取行之后构建逻辑。

$username = mysqli_real_escape_string($con, $_SESSION['username']); 
$query = "SELECT `LTP`, `MSV` FROM `members` WHERE `username`='".$username."'"; 
$data = mysqli_query($con, $query) or die(mysqli_error($con)); 
$row = mysqli_fetch_array($data); 

if ($row['MSV'] == '1') { 
    //provide access to MSV files here. 
} 

if ($row['LTP'] == '1') { 
    //provide access to LTP files here. 
} 

注意,die语句是调试,而不是用于生产用途不够优美。

+0

感谢百万..我会试一试。 – KornholioBeavis