Jump to content

Need some help with build array of a folder


jim.davidson

Recommended Posts

I'm trying to populate a dropdown menu from the names of picture files in a directory.

I'm following some code from a book and it's not working.  I think it because of the variable $theFolder (line 98 of code). Could someone tell me why this code doesn't work?

 

 

 

[attachment deleted by admin]

It's possibly failing because the directory doesn't exist ('../Misc' as set on line 72), but hard to tell for sure without knowing the exact error message.  If you just want a simple bit of code to scan a directory on the server for image files then this should do the trick though:

 

<?php

$dirToScan = '.';  // Directory to scan (current dir)
$imgExts = array('jpg', 'jpeg', 'png', 'gif', 'bmp');  // Array of possibly image file extensions
$imgFiles = array();  // Create blank results array

$allFiles = scandir($dirToScan);  // Get all files in dir into an array
foreach ($allFiles as $filename) {  // Loop through each
$fileInfo = pathinfo($filename);  // Get the info of each file
if (in_array($fileInfo['extension'], $imgExts)) {  // Check to see if file ext is in array
	$imgFiles[] = $filename;  // If so, add the filename to results array
}
}

var_dump($imgFiles);  // Print the results

?>

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.