Solved

List files in folder sub-directories

Posted on 2014-11-22
4
127 Views
Last Modified: 2014-11-22
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
Comment
Question by:peter-cooper
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40459339
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40459347
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
 

Author Closing Comment

by:peter-cooper
ID: 40459914
Thanks very much dave.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40459919
You're welcome, glad to help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now