Jump to content

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

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.