PHP readdir and sort

I make a small gallery. I want to read the file names from the directory and print the file names below, after I have separated some leading digits and file extensions.

I have two versions of the code.

Version 1 does not 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/'),'&nbsp;',$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 is sorted, but I can not manipulate 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 that I'm stupid. Arghhh!

+4
source share
2 answers

EDIT: There are no curly braces in your code

You have

  foreach (...)
       code
       code

and it should be

  foreach (...) {
       code
       code
 }

Just put the code between $ parts and the last $ cfile after the foreach loop, just add curly braces in the loop so you can add more code. Also note that you have different if conditions in both code snippets, you have to decide which one to use or combine them into one condition.

 $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) { //MANIPULATE FILENAME HERE, YOU HAVE $file... if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file)) echo "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n"; } 
+5
source

Since there is not enough space in the comments section ...

Vinko: I am editing here to make it easier. You must have

  $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) { $bfile= substr($file, 2); //strips the first two characters $cfile= preg_replace(('/\d/'),'&nbsp;',$bfile); $cfile= preg_replace(('/_/'),' ',$cfile); $cfile= preg_replace(('/.jpg/'),' ',$cfile); if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file)) // echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n"; //echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n"; $table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n"; //$table_cell .= " <li><a href='" .'pdfs/'. rawurlencode($file) ."'>$file</a></li>\n"; echo $table_cell; } 

instead of what you tried

I tried this:

  $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) $bfile= substr($file, 2); //strips the first two characters $cfile= preg_replace(('/\d/'),'&nbsp;',$bfile); $cfile= preg_replace(('/_/'),' ',$cfile); $cfile= preg_replace(('/.jpg/'),' ',$cfile); if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file)) // echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n"; //echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n"; $table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n"; //$table_cell .= " <li><a href='" .'pdfs/'. rawurlencode($file) ."'>$file</a></li>\n"; echo $table_cell; 

And got this

 <pre> <td><img src='weddings2/36%20And%20they%20lived%20happily%20ever%20after.jpg'><br /> And they lived happily ever after </td></pre> 

Instead of this:

 <pre> <td><img src="weddings2/05Wedding_Chapel.jpg"><br />Wedding Chapel </td> <td><img src="weddings2/06Bride_Flowers.jpg"><br />Bride Flowers </td> <td><img src="weddings2/09%20Bridemaids%20on%20the%20lawn.jpg"><br /> Bridemaids on the lawn </td> <td><img src='weddings2/36%20And%20they%20lived%20happily%20ever%20after.jpg'><br /> And they lived happily ever after </td> </pre> 
0
source

All Articles