• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

chocie random filename in php


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

1258_1-2-3-4.xml
222_1-2-3-4-5-6-7.xml
328_1-2-3-4-5-6-7.xml
429_1-2-3-4-5-6-7-8.xml
263_1-2-3-4-5-6-7-8-9.xml
237_1-2-3-4-5-6-7-8-9-10.xml

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:
429_1-2-3-4-5-6-7-8.xml
263_1-2-3-4-5-6-7-8-9.xml
237_1-2-3-4-5-6-7-8-9-10.xml

3- the system catch random one full name of the 3 files that include the number 8.
0
gsamuk
Asked:
gsamuk
  • 2
1 Solution
 
DVation191Commented:


 
$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

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

Open in new window

0
 
Ray PaseurCommented:
You can read the directory with scandir().  Scandir() returns an array of file names.
http://php.net/manual/en/function.scandir.php

You can get a random number between one and ten with rand().
http://php.net/manual/en/function.rand.php

You can use strpos() to determine if a substring exists inside a longer string.
http://php.net/manual/en/function.strpos.php

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...
// UNTESTED CODE
$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;
}
var_dump($out);

Open in new window

0
 
Chris StanyonCommented:
Give this a go. I think it does what you need
$fileList = array(
	"1258_1-2-3-4.xml",
	"222_1-2-3-4-5-6-7.xml",
	"328_1-2-3-4-5-6-7.xml",
	"429_1-2-3-4-5-6-7-8.xml",
	"263_1-2-3-4-5-6-7-8-9.xml",
	"237_1-2-3-4-5-6-7-8-9-10.xml"
);

$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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now