Shoutbox

Some PHP help, yet again - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Some PHP help, yet again (/showthread.php?tid=92188)

Some PHP help, yet again by Jimbo on 09-12-2009 at 01:28 PM

How would I, using PHP, read every file inside a directory and produce a list of all the files with a "bsp" extension, but not including the extention.
So basically, read a maps/ folder, get every file name, and list it, but only if the extension is a .bsp?


RE: Some PHP help, yet again by Matti on 09-12-2009 at 04:02 PM

PHP code:
<?php
foreach (glob("/path/to/maps/*.bsp") as $file) {
    $info = pathinfo($file);
    echo $info['filename']; // requires PHP 5.2.0 !!!
}
?>

If you're running on PHP < 5.2.0, you can get the file name using substr() and strrpos():
PHP code:
    $info = pathinfo($file);
    $basename = $info['basename'];
    echo substr($basename, 0, strrpos($basename, '.'));


RE: Some PHP help, yet again by Jimbo on 09-12-2009 at 07:44 PM

Thank you Matti :)

How could I then count the number of .bsp files and display that?

Also I am using the following code(very messy, I know) to get all images inside a directory and display them on after another with their names. How could I make them display in a 3 column table, each cell holding an image, and an image name?

PHP code:
Map images:<br><Br>
<?php
$folder = opendir("C:\Inetpub\wwwroot\lgsl\lgsl_files\maps\source\zombie_master");
 
$pic_types = array("jpg", "jpeg", "gif", "png");
 
$index = array();
 
while ($file = readdir ($folder)) {
 
  if(in_array(substr(strtolower($file), strrpos($file,".") + 1),$pic_types))
    {
        array_push($index,$file);
    }
}
 
closedir($folder);
?>
<?php
$path = "C:\Inetpub\wwwroot\lgsl\lgsl_files\maps\source\zombie_master";
$dir_handle = @opendir($path) or die("Unable to open folder");
 
while (false !== ($file = readdir($dir_handle))) {
 
if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file)){
echo "$file<br><img src='http://sammyservers.com/lgsl/lgsl_files/maps/source/zombie_master//$file' alt='$file'><br />";
}
 
 
}
closedir($dir_handle);
 
 
?>


RE: Some PHP help, yet again by absorbation on 09-13-2009 at 12:12 PM

You can use the count(); function for that. Something along the lines of $num_files = count($files_array).


RE: Some PHP help, yet again by Jimbo on 09-14-2009 at 07:22 AM

Thanks, one last thing, how could I compare 2 directories, say a maps directory where all files are a .bsp, and then an images directory where all files are .jpg. How could I compare the filename of these, and then output which .bsp do not have .jpg equivalents?


RE: Some PHP help, yet again by Baggins on 09-14-2009 at 11:29 AM

quote:
Originally posted by Jimbo
Thanks, one last thing, how could I compare 2 directories, say a maps directory where all files are a .bsp, and then an images directory where all files are .jpg. How could I compare the filename of these, and then output which .bsp do not have .jpg equivalents?
http://ca.php.net/manual/en/function.array-diff.php