Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

List files in folder sub-directories

Posted on 2014-11-22
4
Medium Priority
?
142 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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 111

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 84

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

886 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