I'm making a little gallery. I want to read the file names off a directory and print the file names below after I've stripped some leading numerals and file extensions.
I have two versions of the code.
Version 1 doesn't sort
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
$extension = end($parts); // set to we can see last file extension
$bfile= substr($file, 2); //strips the first two characters
$cfile= preg_replace(('/\d/'),' ',$bfile);//remove numbers
$cfile= preg_replace(('/_/'),' ',$cfile);
$cfile= preg_replace(('/.jpg/'),' ',$cfile);
if ($extension == "jpg" OR $extension == "JPG") // is extension ext or EXT ?
echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";
}
}
closedir($dir); // Close the directory after we are done
Version 2 sorts but I can't manipulate the file names
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
$files = array();
while ($files[] = readdir($dir));
sort($files);
closedir($dir);
foreach ($files as $file)
if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))
$table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
echo $table_cell;
Yes, I know I'm dumb. Arghhh!
Since the comment section doesn't have enough room...
Vinko: I'm editing here to make it easier. You should have
instead of what you tried
I tried this:
And got this
Instead of this:
EDIT: Your code is missing braces
You have
and it should be
Just put the code between $parts and the last $cfile after the foreach loop, just add the braces in the loop so you can put more code in. Also note that you have different if conditions in both code snippets, you have to decide which one use or if to combine them both into a single condition.