0
<?php 
if ($handle = opendir('gallery3/var/thumbs/Captain-America/')) { 
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") { 
            echo "$file\n";?><br/><? 
            echo rtrim($file, '.jpg');?><br/><?
            $names[]=$file;
        } 
    } 
    closedir($handle);
}    
?>

I want to createa an array of the file names that are outputting and also the rtrim elements.

3
  • 1
    And what's your problem? Btw, I know everyone (me too) says to not output HTML with PHP, but you don't have to follow it that strictly. Readability is important too! Commented Aug 19, 2011 at 19:29
  • Create an array of $file names and rtrim of $file Commented Aug 19, 2011 at 19:31
  • Seems you already know how to do that. I can see you are adding $file to an array. What prevents you from doing the same with the return value of rtrim($file, '.jpg')? Please be more specific about your question. Give an example of the array you want to get in the end. Commented Aug 19, 2011 at 19:33

1 Answer 1

1

glob (coupled with array_map) may do a better job, but you can create an array, and add matches to it very simply:

$files = array(); // declare
...
$files[] = rtrim($file,'.jpg'); // adding entry
...
print_r($files); // debug output to see it

The glob example:

// with or without the array_map to basename()
$files = array_map('basename',glob('gallery3/var/thumbs/Captain-America/*'));
var_dump($files); // normal files

$files_rtrim = array_map(create_function('$f','return rtrim($f,".jpg");'),$files);
var_dump($files_rtrim); // rtrim version of same files
Sign up to request clarification or add additional context in comments.

2 Comments

glob returns the full path i guess.
You can use basename and avoid that easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.