Jump to content

Use glob to display contents of a folder based on a session variable


amat78

Recommended Posts

I am attempting to us glob to display contents of a users folder using a session variable.

Example: I have a session variable called department

$row_fullname['department'];

 

In department I have the name of the department the user belongs to such as: office, plant, maintenance, and groundskeeping

I created a folder called docs inside of docs there are 4 subfolders called office, plant, maintenance, and groundskeeping

 

I found this code which will display the contents of the folder:

<?php
$files = glob( './docs/office/*.*' );

foreach ( $files as $file )
{
    echo
        '<a href="./docs/office/' . basename( $file ) . '"target="_blank">'
        . basename( $file ) . '</a><br />';
}  
?>

 

The above code works fine, but I would like it to only display the contents of a departments folder only if the user is part on that department.

 

Here is an example that I know is completely wrong but it may help explain what I am trying to do.


<?php
]<?php
$files = glob( './docs/echo $row_fullname['department'];/*.*' );

foreach ( $files as $file )
{
    echo
        '<a href="./docs/echo $row_fullname['department'];/' . basename( $file ) . '"target="_blank">'
        . basename( $file ) . '</a><br />';
}  
?>

Thanks for your time

Link to comment
Share on other sites

You're sort of one the right track. The issue you have is variables are not parsed within single quotes. You should instead concatenate your variable into the string

<?php

$files = glob( './docs/' . $row_fullname['department'] . '/*.*' );

foreach ( $files as $file )
{
    echo
        '<a href="./docs/' . $row_fullname['department'] . '/' . basename( $file ) . '"target="_blank">'
        . basename( $file ) . '</a><br />';
}  
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.