Jump to content

Recommended Posts

The syntax would be....

 

$fileType = Array('jpg', 'JPG', 'jpeg', 'JPEG');
$files = grab_files('./gallery', $fileType);

 

I'm not sure where people get the idea that variables need quotes around them.

 

However....

 

You can't just hand any old function an array and expect it to work. You would likely need to customize the function to work with an array is input.

In future, please either post the source of the function (in the thread, in a pastebin or as a link to it: e.g. http://www.phpfreaks.com/forums/index.php?topic=278778.0) so that we can see how it works, since it's not a core PHP function.

 

For this particular issue (accepting an array of allowable extensions) the line reading if(pathinfo($file, PATHINFO_EXTENSION) == $ext) inside the grab_files function will need to be amended to compare the file extension against the provided array.

code still doesnt work even without the quotes =[

 

 

nd hey salathe,

this is the source of the function:

 

<?php
$start_dir = "./gallery_and_purchase";
function grab_files($start_dir, $ext)
{
$dir = new RecursiveDirectoryIterator($start_dir);
$files = Array();
foreach($dir->getChildren() as $file)
if (stristr($file, "large")===FALSE)
if(is_dir($file))
$files = array_merge($files, grab_files($file, $ext));

else
if(pathinfo($file, PATHINFO_EXTENSION) == $ext)
$files[] = (string) $file;
return $files;
}

$fileType = Array('jpg', 'JPG', 'jpeg', 'JPEG');
$files = grab_files('./gallery', $fileType);
shuffle($files); array_splice($files, 15);
foreach($files as $file)
{
$file = str_replace("\\.\\","/",$file);
$fileSplit = explode("/", $file);
	$fileName = $fileSplit[3];
$fileName = substr("$fileName", 0, -4);
echo $fileName;
}
?>

SOLVED:

 

<?php
$start_dir = "./gallery";
function grab_files($start_dir, $ext)
{
$dir   = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir));
$files = array();
// Force array of extensions and make them all lower-case
if ( ! is_array($ext))
{
$ext = (array) $ext;
}
$ext = array_unique(array_map('strtolower', $ext));
foreach($dir as $file)
{
// Skip anything that isn't a file
if ( ! $file->isFile());
continue;

// If the file has one of our desired extensions, add it to files array
if (in_array(strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), $ext)) {
$files[] = $file->getPathname();
}
}
return $files;
}
// Grab all .jpg files recursively
$fileType = Array('jpg', 'jpeg');
$files = grab_files('./gallery', $fileType);

// Get random keys
$keys = array_rand($files, min(20, count($files)));
foreach($keys as $key)
{
echo $files[$key] . "<br>\n";
}
?>

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.