how to read file names within a folder?whats wrong with my code – PHP

  php

Q(Question):

how can i get the php to read the all the file names within a folder and put the names into an array?

A(Answer):

If you can set the folder up as an FTP location then ftp_nlist() does exactly this.
Otherwise.. I think I have done this somewhere!? Will try to find relevant code

A(Answer):

Found it

public static function getFiles($folder,$ignore='')
{
$filenames = false;
// Remove any trailing slash
if(substr($folder, -1) == '/')
$folder = substr($folder, 0, -1);
//Can't remember why all these tests are here
if(file_exists($folder) || !is_dir($folder) || !is_readable($folder))
{
// Grab a file handle
if($handle = opendir($folder))
{
$filenames = array();
while(false !== ($file = readdir($handle)))
{
//Ignore folders in the directory
if(!is_dir($folder.'/'.$file))
{
//Skip DOS shortcuts
if(!(!strcmp($file,'.') || !strcmp($file,'..')))
{
//Skip extensions in ignore
if(!empty($ignore))
{
foreach($ignore as $ext)
{
if(strpos($file,$ext)===false)
$filenames[] = $file;
}
}
else $filenames[] = $file;
}
}
}
}
else throw new Exception('Could not open '.$folder);
}
else throw new Exception('Folder does not exist '.$folder);
closedir($handle);
sort($filenames);
return $filenames;
}

A(Answer):

altering as the code i put here was wrong and i figured the answer myself

A(Answer):

but i m getting error please help

Difficult to fix an error when you don’t tell people what it is

A(Answer):

@code green

yes codegreen my mistake but initally i had had the part displaying the title and body for single file when i put the $filename directly …but i wanted to broaden the horizen and make rewrite for all the files within the directory by reading it..but its giving me the problem then.but thanx for the code i did use parts of it .

A(Answer):

ok no need …i figured out the problem and sorted it out too…thanx

LEAVE A COMMENT