LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2007, 12:02 AM   #1
latino
Member
 
Registered: Aug 2003
Location: Puerto Rico
Distribution: Centos 6.6
Posts: 142

Rep: Reputation: 15
Cool php random file from dir


Hi:

I need to be able to rotate or random read the contents of slider directory. This in php since it woill be used in the web. Right now I have the code below

$sliderFile = "system/cache/slider/".$_GET["id"].".dat";

I managed to hardcode a specific file. But I am interesting in some kind of rotation beetwen file sin the slider dir or random choose of ém.

Any help will be appreciated!!

 
Old 07-05-2007, 12:43 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Conceptually, store list of filenames in array by number.
Generate random num in range 0 - (num array elements -1)
retrieve filename at array[random_num]
 
Old 07-05-2007, 05:56 AM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
I'm really not sure what you're trying to do, your post is really unclear. But, I trust that it's what chrism01 describes. So here goes.

PHP Code:
<?
    $directory
="path/to/directory";
    
$all_files=scandir($directory);
    if(
is_array($all_files))
    {
        echo(
$directory."/".$all_files[rand(0,count($all_files))]);
    }
?>
Haven't tested that, but I guess you can do that. And by the way, scandir also returns "." and ".." as file names. If you want to get rid of them, do array_shift twice. Cheers.
 
Old 07-06-2007, 02:20 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
do they (., ..) always come first in output ? are you sure? (me no PHP programmer)
 
Old 07-06-2007, 10:45 AM   #5
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
You are right chrism, they don't always come in first, thank you for pointing that out.

As the documentation for scandir states, they come in a sorted order. So, if we have file names beginning with !,$,%,&,*, and so on they will come up before '.' and '..' , thus creating a problem. One solution would be avoiding these file names .

Another would be array_search and then checking in the foreach or, if this makes the efficiency daemon within you anxious, you can use array_splice to reduce them, or something else..

Anyway, I recommend only using decent characters for file names, it's by far the most efficient solution. Cheers.
 
Old 07-07-2007, 03:55 PM   #6
latino
Member
 
Registered: Aug 2003
Location: Puerto Rico
Distribution: Centos 6.6
Posts: 142

Original Poster
Rep: Reputation: 15
Hi

Thank you for the reply. But How I select a random file from that directory? See what I want:


$sliderFile = "system/cache/slider/".$_GET["id"].".dat";

The slider directory have these files:

1.dat
168.dat
34.dat
38.dat
489.dat

The number is the user id of the cms I am using. I want to be able to random select one of those files to made the row above look like:

$sliderFile ="system/cache/slider/randomfile_selected.dat";

So the above string some times will have

$sliderFile = "system/cache/slider/38.dat";
$sliderFile = "system/cache/slider/489.dat";

and so on...

Thanks again!

 
Old 07-08-2007, 10:08 PM   #7
latino
Member
 
Registered: Aug 2003
Location: Puerto Rico
Distribution: Centos 6.6
Posts: 142

Original Poster
Rep: Reputation: 15
Hi:

The final code and fix is this.

PHP Code:
$directory="LOCAL ADDRESS OF DIRECTORY";
$all_files=scandir($directory);
if(
is_array($all_files))
{
$sliderFile $directory."/".$all_files[rand(0,count($all_files))];

Thanks!

 
Old 07-09-2007, 02:26 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
n3cb: I did wonder; in Perl it's std to say:

next if( $file =~ /^\.\.?$/ );

or variations thereon eg:

next unless -f "${evts_dir}/${evt_file}"; # regular files only
 
Old 07-09-2007, 03:47 AM   #9
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Yup, that's really nice. I've been thinking of learning perl, I might just do it.
 
Old 07-09-2007, 06:04 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Here ya go: http://perldoc.perl.org/
 
Old 07-09-2007, 03:33 PM   #11
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Thank you
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
Compress dir keep file/dir permissions powadha Linux - General 1 11-14-2006 07:07 PM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 07:16 AM
PHP - read a random line from a file? Erik Thorsson Programming 3 12-10-2004 10:31 AM
upload dir with php retrodict Programming 0 12-08-2004 02:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration