Link to home
Start Free TrialLog in
Avatar of Pierre K
Pierre KFlag for South Africa

asked on

Populate listbox with filenames on webserver in PhP/HTML

Hi there - I have the following script which I'm trying to get to display all file names inside the folder named 'leave' located on the root where the php file is located.

If I add the '123' as file name I et nothing (but there is a test file named 123.xml)
Without the 123 (just .xml) I get loads too many files and also duplicates since the same file names might appear in diffrent folders
I assume the level has to do with that.
Can someone tweak this script please?
Thanks

 <?php
function searchSite( $path = '.', $level = 0 ){

$skip = array( 'cgi-bin', '.', '..' ); //these may choke it
$look_for = array( '123.xml' );//files to look for
    
    $dh = @opendir( $path ); //open the first level folder specified in function call
    
    while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory
    $file_ext = substr($file,-4,4);
        if( !in_array( $file, $skip )){    // Check that this file is not to be skip
            if( is_dir( "$path/$file" ) ){  // If dir, read...
                searchSite( "$path/$file", ($level+1) );
            } else if (in_array($file_ext,$look_for)){
                echo "<option value='$path/$file' />$file</option>";
        }
            }
        }
    closedir( $dh );// Close the directory handle
}
?>

<p>List Box - Single Select<br>
<select name="listbox" size="1">
<option  selected>Click here to see leave history</option>
<?php
searchSite(".");
?>
</select>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Here is my teaching example script that shows how to find all of the error_log files, recursively, throughout the directory tree.  Maybe you can adapt it for your needs.  I use it to check for any PHP errors and display the error logs, and/or optionally delete them.
<?php // find_error_log.php
error_reporting(E_ALL);
ob_start();

// PUT THIS SCRIPT IN THE WEB ROOT DIRECTORY
$path = realpath(getcwd());
$plen = strlen($path);

// THE ERROR LOG FILE NAME
$signal = strrev('error_log');


// IF THERE IS A POST-METHOD REQUEST TO DELETE THIS ERROR LOG
if (!empty($_POST['log']))
{
    // MAKE SURE WE ONLY UNLINK THE ERROR LOG FILE
    $test = strrev($_POST['log']);
    if (strpos($test, $signal) === 0)
    {
        unlink($path . $_POST['log']);
        echo '<h3>' . $_POST['log'] . ' Discarded</h3>';
    }
}


// SEE http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// UNFORTUNATELY THIS DOES NOT WORK AT PHP 5.4
// var_dump($objs);

// ITERATE OVER THE OBJECTS
foreach($objs as $name => $obj)
{
    // PROCESS THE ERROR LOG ONLY
    $test = strrev($name);
    if (strpos($test, $signal) === 0)
    {
        $name = substr($name, $plen);
        $form = <<<EOD
<form method="post" style="margin:0; padding:0; display:inline;!important">
<b>$name</b>
<input type="submit" value="Discard?" />
<input type="hidden" name="log" value="$name" />
</form>
EOD;
        echo $form;

        // SHOW THE CONTENTS OF THE ERROR LOG
        echo '<pre>';
        print_r(file_get_contents($path . $name));
        echo '</pre>' . PHP_EOL;
    }
}

$out = ob_get_contents();
if (empty($out)) echo '<h3>Good News! No error_log found.</h3>';
echo '<h3><a href="' . $_SERVER['PHP_SELF'] . '">Try again?</a></h3>';

Open in new window

Avatar of hielo
>> If I add the '123' as file name I get nothing
Are you trying to get a listing of all files that begin with 123 at any directory sub-level? -ex: 123.txt, 123.xml, 123.html
Or are you interested only for 123.xml at any directory sub-level?
Avatar of Pierre K

ASKER

I apologize for my lack of PhP knowledge, I'm a VBA Developer trying my hand at this.
Your script is way to complex for my knowledge level to try and change. I had a few looks but not making progress.

Do you mind testing my script (I'll actually post it here again in it's original form) and maybe you can find a workable solution:
1. Like your code, the PhP script will also reside on the root along with a folder named "Leave" which will contain all the XML files that I want to list inside the listbox.

2. The code below is very close to what I need except that it doesn't ONLY look inside the "Leave" folder the way I need it to do

3. And I need a way to specify the file name and extension I wanted listed as the file name will later become the Staff member's code so that only his/her files will be displayed.
Eg: If he is Staff number 123 then only file names staring with 123 will be displayed like 123 - Annual.xml and 123 - Sick.xml.
Like a wildcard "*" after the Staff code

But for now point 1 and 2 above will be fine and I'll play around and post a new question regarding point 3 if need be

 <?php
function searchSite( $path = '.', $level = 0 ){

$skip = array( 'cgi-bin', '.', '..' ); //these may choke it
$look_for = array( '.jpg', '.gif', '.png' );//files to look for
    
    $dh = @opendir( $path ); //open the first level folder specified in function call
    
    while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory
    $file_ext = substr($file,-4,4);
        if( !in_array( $file, $skip )){    // Check that this file is not to be skip
            if( is_dir( "$path/$file" ) ){  // If dir, read...
                searchSite( "$path/$file", ($level+1) );
            } else if (in_array($file_ext,$look_for)){
                echo "<option value='$path/$file' />$file</option>";
        }
            }
        }
    closedir( $dh );// Close the directory handle
}
?> 

<p>List Box - Single Select<br>
<select name="listbox" size="1">
<option  selected>Click here to see them by themes</option>
<?php
searchSite(".");
?>
</select>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@hielo - see my comment to the prev post and point 3:

So yes - I need to see only XML file types residing in the folder named "Leave" residing on the root and then the name must be the $_Session['username'] followed by a wildcard to give me just the logged-in User's files:
123 - Annual.xml
123 - Sick.xml

Etc

Hope this is clear :)
Ahh, new question information.  I'll try to look at the new example now.
Just a note - the PHP session is described in this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

All PHP variables are case-sensitive.  $_SESSION != $_Session (a common point of confusion).
@Ray - you are very close - just give me a few more minutes to test your most recent code...
This is a little more of a "black box" implementation.  You should be able to set your own variables at the places noted in the comments (I only used these directory and file patterns because it was what I had to test with).
<?php // demo/temp_pierrek.php

/**
 * http://www.experts-exchange.com/questions/28699216/Populate-listbox-with-filenames-on-webserver-in-PhP-HTML.html
 * http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
 * http://php.net/manual/en/class.splfileinfo.php
 *
 * Put this script in the top-level directory
 */
error_reporting(E_ALL);


/**
 * THE DESIRABLE FILE NAME IS IDENTIFIED HERE
 */
$signals = 'earth';


/**
 * THE SEARCHABLE DIRECTORY IS IDENTIFIED HERE
 */
$path = 'images';


// THE OPTION STATEMENTS ARE COLLECTED IN AN ARRAY
$options = [];

// COLLECT THE FILE OBJECTS
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// ITERATE OVER THE OBJECTS TO FIND THE DESIRABLE FILES
$rgx = '#' . preg_quote($signals) . '#';
foreach($objs as $path => $obj)
{
    $name = $obj->getFileName();
    if (preg_match($rgx, $name))
    {
        $options[]
        = '<option value="'
        . $path
        . '">'
        . $name
        . '</option>'
        ;
    }
}

// GENERATE THE OPTION STATEMENTS
$opts = implode(PHP_EOL, $options);

// CREATE THE HTML USING HEREDOC NOTATION
$html = <<<EOD
<p>List Box - Single Select<br>
<select name="listbox" size="1">
<option value="">Choose a File</option>
$opts
</select>
</p>
EOD;

// SHOW THE HTML
echo $html;

Open in new window

Also, if you have some programming background and want to learn PHP, this article can help you find many good learning resources, and avoid the many terrible PHP examples that litter the interwebs.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
@Ray - wow you are too fast for me :) Still testing your prev code:
The only thing I changed was this line:
$signals = ['123.xml'];
As you can see I hard-coded the 123 into the line and then it works.
Would now like to know how to expand it a bit with the wild card idea.
So actually you answered my initial question but since you posted new code just now - let me at least test that before I reply again ok
(I'm a EE Noob)
Thanks for this piece of code. I only had to change one line to give me the list box I need.
I will post a new question later on regarding the wild card option
... expand it a bit with the wild card idea ...
That's what regular expressions are good for.  This is a learning "rabbit hole" that people have written entire books about.  Here is the PHP man page:
http://www.php.net/manual/en/book.pcre.php

There are lots of ways to process string variables in PHP.
http://php.net/manual/en/book.strings.php
Thanks a lot Ray!!
10-4, glad to help!
PS: So far I got it to recognize the current username for the session so all I now have to do is read up on the syntax for the wild card and I'm done.
Enjoy your evening/day - not sure timezone :)
PS: No need to answer I do realize the question session is closed. Just excited about the progress I made :)