Skip to main content

Looping through directory and list files after sorting - php function


PHP Code for simply looping and listing the files names in a directory into a select box.


<select>
<?php

$logFolder="../csvfolder";
if ( $handle = opendir($logFolder) ) {
/* loop over the directory. */
while (false !== ($file = readdir($handle))) {
if( stripos( $file,".csv" ) > 0 )
echo "<option value=\"$file\">$file</option>\n";
}

closedir($handle);
}

?>
</select>


Here I am checking whether the file in the folder is a csv file, if it is a csv it is listed.




PHP function to loop through the files in a directory and list the file names after sorting it.


<ul>
<?php
$dir = "directory";
if ($handle = opendir ($dir)) {
while (false !== ($file = readdir ($handle))) {
if (preg_match ("/.inc$/i", $file)) {
$file = str_replace ("_", " ", $file);
$file = explode (".", $file);
$file = $file[0];
$filelist[] = trim ($file);
}
}
sort ($filelist);
reset ($filelist);
while (list ($key, $val) = each ($filelist)) {
print (" <li><a href=\"temp.php?type=$val\">$val</a></li>\n");
}
closedir ($handle);
}
?>
</ul>








A Php function you can use to list the files in the directory you specify, their type (dir or file) and whether they are hidden.

Modify it to do other things.


<?php
function listDirs($where){
echo "<table border=\"1\"><tr><td><b>Name</b></td><td><b>Type</b></td>";
echo "<td><b>Invisible (Hidden)?</b></td></tr>";
$itemHandler=opendir($where);
$i=0;
while(($item=readdir($itemHandler)) !== false){
if(substr($item, 0, 1)!="."){
if(is_dir($item)){
echo "<tr><td>$item</td><td>Directory</td><td>No</td></tr>";
}else{
echo "<tr><td>$item</td><td>File</td><td>No</td></tr>";
}
$i++;
}else{
if(is_dir($item)){
echo "<tr><td>$item</td><td>Directory</td><td>Yes</td></tr>";
}else{
echo "<tr><td>$item</td><td>File</td><td>Yes</td></tr>";
}
$i++;
}
}
echo "</table>";
}
?>


//Then call it like this.


<?php
listDirs(DIR);
?>


//Example:
<?php
listDirs("/tests/directorylisting");
?>





Comments

Popular posts from this blog

Payback Points - How to redeem - How to merge multiple payback accounts - Block Payback card - Payback customer care

Your SBI Debit card ending with XX0000 is deactivated only for Internet txn.

SBI account holders may have received an SMS with following message, supposed to be from State Bank of India (SBI).

Your SBI Debit card ending with XX0000  is deactivated only for Internet txn. To activate send SMS "SWON ECOM 0000" to 09223966666. No change for ATM/POS usage
** Replace the four Zeros with last 4 digits of your debit card number

Recently many of the SBI account holder has losed their money due to a hi-tech ATM robbery which happened in Thiruvananthapuram, capital city of Kerala.

How to turn off BSNL Buzz services - Steps to deactivate BSNL Buzz messages

BSNL Buzz is a service from BSNL for mobile customers, in which when your phone is in idle mode, teasers or small buzz messages related to topics like entertainment, lifestyle, sports, business, fun, polls, daily news updates are delivered to your mobile. When you activate a BSNL SIM card, through which BSNL BUZZ is available, you automatically accept the terms and conditions for teaser delivery.

Recently, I bought a new BSNL SIM card. After the activation, when ever I unlock the phone, I was getting lots of buzz message one after another. I have to click on cancel / back button multiple time to view the home screen. Some times when some call arrives, it was difficult to attend calls due to this teaser popups. So I decided to deactivate the BSNL buzz. Here are the steps to Deactivate or Turn Off BSNL Buzz.


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog