Jump to content

[SOLVED] LOAD TEXT FILES FROM A FOLDER


andz

Recommended Posts

I have a folder called "samples" which contains the following text files array("test1.txt", "test2.txt", test3.txt");

 

I know a code to open a certain file, browse and edit its content.

 

I need a code or is it possible for me to load random text files 1 at a time based on the text files above from the samples folder???

 

It's like mapping the whole directory and randomly open different text file every time the page reloads no matter how many files were there on the samples folder.

Link to comment
https://forums.phpfreaks.com/topic/128796-solved-load-text-files-from-a-folder/
Share on other sites

$files = shuffle(glob("*.txt"));

Should do it.

 

Scott.

 

Could you create a full sample out of this code?

 

I just used this to my existing code.

 

$file = shuffle(glob('SAMPLES/*.txt'));

 

$fileOpen = fopen($file, 'r');

 

$content = fread($fileOpen, filesize($file));

 

echo $content;

 

I only get the value of 1 as an output.

 

 

 

try this

$files = shuffle(glob('SAMPLES/*.txt'));
foreach($files as $filename ){
    $fileOpen = fopen($filename , 'r');
    $content = fread($fileOpen, filesize($filename ));
    echo "Content of:".$filename ."<br>".$content;
}

glob() will return an array of all files matting the pattern.

shuffle() will randomize the array.

 

Scott.

you can shuffle the array first

srand((float)microtime() * 1000000);
$array = array("test1.txt", "test2.txt", "test3.txt");
shuffle($array);
foreach ($array as $file {
    echo "$file\n ";
}

 

 

I've written a code out of those suggested above including yours. Please look below

 

<?php

srand((float)microtime() * 1000000);

$files = glob("adverts/wide/*.txt");

 

shuffle($files);

 

foreach ($files as $file) {

// file name

echo $file . '<br />';

 

$filename = $file;

$handle = fopen($filename, 'r');

$contents = fread($handle, filesize($filename));

 

// output the contents here

echo $contents;

fclose($handle);

}

?>

 

Is there a way to LIMIT that only 1 text file will be browse?

 

Thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.