Jump to content

[SOLVED] I want to turn filenames into values in an array....


supamastadan

Recommended Posts

Hello.  As an introduction, my name is Dan, and I am by no means an expert in php.  In fact, my php experience has consisted of me modifying free, creative licensed scripts to meet my needs and learn as I go.  That being said, it's worked up untill this point.  And while I've searched for an answer online, I haven't quite figured out the solution to my prediciment and hope that fresh eyes and deeper PHP experience will find something simple and easy that I haven't thought of.  So with no further adue, here we go...

 

I have a directory with a collection of image files in it.  All of which are labeled in a date format.  I.E. 2008-02-25.jpg.  What I'm trying to do is take each of those files and make those dates values in an array, to be used to link dates on a calendar.  I managed to get a script that will print the file names properly.

 

<?php $dirname = "strips";
$dh = opendir( $dirname ) or die("couldn't open directory");

while (!(( $file = readdir( $dh ) ) === false ) ) {
     if ( is_dir( "$dirname/$file" ) )
          print "";
     $mod = substr_replace ($file, "", 10, 4);
     print "$mod<br>";
}
closedir( $dh );
?>

 

Ok... so this is where greenhorn-ness shines through.  While the print displays correctly modified strings, I have no clue how to make each file name a value in an array.  Is the code I have even the most efficient way to go about doing this?  I'm sure there are others, maybe even better ones.  Let me know what you think.  Thanks a lot for your help and have a nice day.

 

- Dan

<?php

$dirname = "strips";

$dh = opendir( $dirname ) or die("couldn't open directory");

 

while (!(( $file = readdir( $dh ) ) === false ) ) {

if ( is_dir( "$dirname/$file" ) )

print "";

$mod = substr_replace ($file, "", 10, 4);

print "$mod<br>";

$newArray[] = $mod;

}

closedir( $dh );

?>

 

That should work... I have tried to go back in a few times and add code tags.. but the quick edit feature does not seem to like that idea.

 

Hope that helps

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.