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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.