<?php
session_start();
error_reporting(E_ALL);
// THE DESIRABLE FILE NAMES ARE KEPT IN AN ARRAY
//should be able to find 123*.xml
//$signals = Array($_SESSION['username'].'*.xml'=>'regex');
//$signals = Array($_SESSION['username'].'.*\.xml'=>'regex');
$signals = Array( '^' . $_SESSION['username'].'.*\.xml'=>'regex');
// THE OPTION STATEMENTS ARE COLLECTED IN AN ARRAY
$options = Array();
// COLLECT THE FILE OBJECTS
$path = realpath(getcwd());
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
// ITERATE OVER THE OBJECTS TO FIND THE DESIRABLE FILES
foreach($objs as $path => $obj)
{
$name = $obj->getFileName();
// this is looking for "exact" matches.
if (in_array($name, $signals))
{
$options[]
= '<option value="'
. $path
. '">'
. $name
. '</option>'
;
}
else
{ //echo 'Searching for '.$name.PHP_EOL;
foreach($signals as $k=>$v)
{
// is you set $signals = [ '123*.xml/'=>'regex', '456*.txt'=>'regex']
//the if clause below will get executed
if( $v=='regex' )
{
if( preg_match('/'. str_replace('/','\\/',$k) .'/',$name))
{
$options[]
= '<option value="'
. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
. '">'
. substr($name,strlen($_SESSION['username']),strlen($name)-4-strlen($_SESSION['username'])) //Only pass file name without ext
. '</option>'
;
}
}
// is you set $signals = [ '123'], but you have the following files: '123.xml', 'xyz-123.txt'
// the if clause below will get executed and will find both, because this is a "contains" filter
elseif( stripos($name, $v)!==false )
{
$options[]
= '<option value="'
. str_replace($_SERVER['DOCUMENT_ROOT'],'', $path)
. '">'
. $name
. '</option>'
;
}
}
}
}
// GENERATE THE OPTION STATEMENTS
$opts = implode(PHP_EOL, $options);
// CREATE THE HTML USING HEREDOC NOTATION
$html = <<<EOD
<p>Leave History<br>
<select name="listbox" size="1">
<option value="">Open to view</option>
$opts
</select>
</p>
EOD;
// SHOW THE HTML
echo $html;
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE