chocie random filename in php

Posted on 2011-10-21
Medium Priority
Last Modified: 2012-05-12

friends i hang out,
i have create files via php like :


my Question: i draw a random number between 1 and 10 and want to select the full name of the file or files random who got the number that i draw.

example :

1- i draw the number 8

2- the system find 3 files that got the number 8 after "_" in the file name like those 3:

3- the system catch random one full name of the 3 files that include the number 8.
Question by:gsamuk
  • 2
LVL 20

Expert Comment

ID: 37005868

$file = 'image.jpg';
$info = pathinfo($file);
$file_name =  basename($file,'.'.$info['extension']);

$rand = rand(1, 10); 
if(strpos($file_name, $rand) > 3)
 $array[] = $file_name;

Open in new window

LVL 20

Expert Comment

ID: 37005874
Oh and then you wanted a random array element:
$rand_keys = array_rand($array, 1);
echo $array[$rand_keys[0]];

Open in new window

LVL 111

Expert Comment

by:Ray Paseur
ID: 37005991
You can read the directory with scandir().  Scandir() returns an array of file names.

You can get a random number between one and ten with rand().

You can use strpos() to determine if a substring exists inside a longer string.

Your sample file names have both the dash and the underscore in them.  These are different characters. So the requirement to find "the number 8 after "_" in the file name" will not be satisfied by any of the test data.  However if we assume that the actual test data used underscores instead of dashes between the numbers, you would do something like this...
$dir = scandir("path/to/your/directory");
$num = rand(1,10);
$str = '_' . "$num";
$out = array();
foreach ($dir as $file_name)
    if (strpos($file_name, $str) !== FALSE) $out[] = $file_name;

Open in new window

LVL 45

Accepted Solution

Chris Stanyon earned 2000 total points
ID: 37008422
Give this a go. I think it does what you need
$fileList = array(

$matchedFiles = array();
//get a random number between 1 and 10
$rand = rand(1, 10);

//Loop through the list of files
foreach ($fileList as $file) {
	//split the filename into parts so we can deal with just the 'numbers'
	$fileParts = explode("_", substr($file,0, strlen($file)-4));
	$fileNumbers = explode("-", $fileParts[1]);

	//If the filename contains the random number, add it to an array
	if (in_array($rand, $fileNumbers)) $matchedFiles[] = $file;

//Now return a random file from the matched list.
$randFileNumber = rand(1, count($matchedFiles)) - 1;

echo "<p>Random Number: " . $rand . "</p>";
echo "<p>Random File: " . $matchedFiles[$randFileNumber] . "</p>";

Open in new window


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question