Asked  7 Months ago    Answers:  5   Viewed   32 times

I have one directory called images/tips.

Now in that directory I have many images which can change.

I want the PHP script to read the directory, to find the images, and out of those images found to pick a random image.

Any idea on how to do this?

 Answers

71
$imagesDir = 'images/tips/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments

You can send a 2nd argument to array_rand() to get more than 1.

Wednesday, March 31, 2021
 
AlterPHP
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
48

Get rid of the

$objReader->setReadDataOnly(true);

otherwise PHPExcel has no way of knowing whether a cell is formatted as a date or not

setReadDataOnly(true) tells PHPExcel to load only the raw data from each cell. As the only way MS Excel differentiates a date/time from a float is through the number format mask (not from the raw data), and you're telling PHPExcel not to read the number format masks, PHPExcel can't tell the diference, so all floats are simply treated as floats

Saturday, May 29, 2021
 
rorymorris
answered 5 Months ago
60

You can use glob

$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);

If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos, fnmatch or pathinfo to get the extension is up to you.

Thursday, June 3, 2021
 
Shobit
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :