• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

List files in folder sub-directories

I have a script that when clients create a pdf file, it is placed in a folder named with there own code. This is in a main folder called 'destcerts' and the folder structure ends up looking like the illustration below. What I need to do as admin is have access to these files and the only way I can do that at the moment is to hard code the sub-directory in the code and that is not practical for obvious reasons.

What I thought I could do is, have a select dropdown with a list of clients which when selected could trigger perhaps a change event which would say loop for the chosen client and display the files. Unfortunately, my php or jquery is not good enough to achieve this and I would be grateful if someone could show me the way to g forward with this. Many thanks

<div id="viewCerts" style="display:none;">
      <?php
        $sub = 'destcerts/';
        
        // READ THE NAMES OF FILES IN THE SUB-DIRECTORY
        $fff = new DirectoryIterator($sub);
        $sss = array();
        
        foreach ($fff as $filedata)
        {
        // SKIP THE "DOT" FILES
        if ($filedata->isDot()) continue;
        
        // ACTIVATE THIS LINE TO RESTRICT IT TO PDF FILES ONLY
        if ($filedata->getExtension() != 'pdf') continue;
        
        // SET AN INDICATOR TO SHOW WE FOUND DATA
        
        
        // CREATE LINKS TO THESE FILES
        $nom = $filedata->getFilename();
        //$value = substr ($nom, 0, 4);
        $_SESSION['value'] = $nom;
        //if($value == 'DEMO') {
        
        $lnk
        = '<img src="destcerts/PDF_icon_100.png" style="margin-bottom: 15px; margin-top:15px;"><br /><a href="'
        . $sub
        . '/'
        . $nom
        . '" style="color:#0099FF; text-decoration:none; font-size:12px; font-family: Verdana, Geneva, sans-serif;">'
        . $nom
        . '</a>'
        ;
        
        //} 
        
        // COLLECT THE LINKS HERE
        $sss[] = $lnk;
        
        }
        
        // ACCUMULATE THE TABLE ROWS HERE
        $trs   = NULL;
        
        // COLLECT GROUPS OF FOUR
        If(!empty($sss)){
        while (!empty($sss))
        {
        $td1 = array_shift($sss) or NULL;
        $td2 = array_shift($sss) or NULL;
        $td3 = array_shift($sss) or NULL;
        $td4 = array_shift($sss) or NULL;
        
        // USE HEREDOC TO INSERT THESE INTO A TABLE ROW
        $tr  = <<<EOD
        <tr>
        <td align="center" width="20%" style="padding-bottom:20px !important;">$td1</td>
        <td align="center" width="20%" style="padding-bottom:20px !important;">$td2</td>
        <td align="center" width="20%" style="padding-bottom:20px !important;">$td3</td>
        <td align="center" width="20%" style="padding-bottom:20px !important;">$td4</td>
        </tr>
EOD;
        
        // APPEND THE TABLE ROW TO THE OTHER ROWS
        $trs .= $tr;
        }
        }
        else{
        
        $msg = "There are no files to display";
      
        $tr  = <<<EOD
        <tr>
        <td style="text-align:center; padding: 10px !important; font-weight: normal;">$msg</td>
        </tr>
EOD;
        
        $trs .= $tr;
        }
        
        // USE HEREDOC TO INSERT THE TABLE ROWS INTO THE TABLE
        $tab = <<<EOD
        <table id="pdfDownload" width="97%" align="center" border="1" cellspacing="10" cellpadding="0" style="border:1px solid grey; padding-bottom: 10px; margin-bottom:20px;">
        <th style="text-align:center; padding: 10px !important; padding-bottom: 20px; border:1px solid black; background-color: #3399FF; color: white; font-size: 18px !important;" colspan="4">Destruction Certificates Download</th>
        <tr>
        <th></th>
        </tr>
        $trs
        </table>
EOD;
        
        // SHOW THE WORK PRODUCT
        echo $tab;
?>
    </div>

Open in new window

.
..
destcerts
  client1
  client2
  client3
  etc

Open in new window

0
peter-cooper
Asked:
peter-cooper
  • 2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Here is a php code that will list the files in the current directory plus a list of the sub-directories.  It doesn't have a drop-down and you will probably have to make some changes for your use.  Put it in 'destcerts' and run it.
<?php
// file extensions that will be recognized
$extstr = " php html asp aspx";
$parts = array();
$listing = array();
$dirs = array();

if ($handle = opendir('.')) {
		$j = 0;
    while (false !== ($dlist[$j] = readdir($handle))) {
        //if ($file != "." && $file != "..") $j++;
        if ($dlist[$j] != "." && $dlist[$j] != "..") $j++;
				}
		//sort($dlist, SORT_STRING);
		natcasesort($dlist);
		foreach ($dlist as $key => $val) {
		$parts = explode(".",$val);
		if(isset($parts[1]) && (strpos($extstr, $parts[1]) > 0)) {
			if(!isset($parts[2])) {
    		$listing[] = $val;
				}
			}
		if(is_dir($val)) $dirs[] = $val;
		}
    closedir($handle);
		//sort($listing, SORT_STRING);
		natcasesort($listing);
		$Lencnt = count($listing);
		$clng = 27;
		$divsr = ceil($Lencnt/$clng);	
		//$divsr = ($Lencnt/$clng);	
		echo $Lencnt." : ".$divsr."<br>\n";
		$Lenlist = $Lencnt/$divsr;
		echo $Lenlist."<br>\n";
		echo '<table border="1" cellpadding="7px" cellspacing="0" summary=""><tr><td valign="top">';
		foreach ($listing as $val) {
   		echo '<a href="' . $val . '">' . $val . "<br>\n";
			$Lenlist = $Lenlist-1;
			if($Lenlist < 0) {
				echo '</td><td valign="top">';
				$Lenlist = $Lencnt/$divsr;
				}
		}
		echo '</td><td valign="top"><b><u>Directories</u></b><br><br>';
		$Lenlist = $Lencnt/$divsr;
		foreach ($dirs as $val) {
   		echo '<a href="' . $val . '">' . $val . "<br>\n";
			$Lenlist = $Lenlist-1;
			if($Lenlist < 0) {
				echo '</td><td>';
				$Lenlist = $Lencnt/$divsr;
				}
		}
		
		echo '</td></tr></table>';
	}
?> 

Open in new window

0
 
Ray PaseurCommented:
I think you need to consider hiring a professional application developer who can get "hands-on" with your servers and data.  This is not really a question with an answer - it's a requirement for application development that is specific to your organization, client list, and data structures.  It's a simple application - we've all done these things before - and it will not cost you very much money to get a professional solution.

If you have the time and interest and want to learn enough PHP to become a developer yourself, this article can help you get started.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Best of luck with it, ~Ray
0
 
peter-cooperAuthor Commented:
Thanks very much dave.
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now