chocie random filename in php

Posted on 2011-10-21
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
    LVL 20

    Expert Comment


    $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

    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 107

    Expert Comment

    by:Ray Paseur
    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 42

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    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…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now