Jump to content
xisto Community
Sign in to follow this  
matak

Reading Files From Directory To Array, And Using $_get To Get Them Simple way to manage lots of files

Recommended Posts

Some user posted a similar problem i had when i tried to figure out how to update content on my website in less work as possible. This is just part of the "big" plan i have for my site but it can be helpfull to you guys if you like FlatFile. I hope that mod's don't mind me posting the same code here and on their forums, couse if they do i'll delete it from their forums ;)

So here's the situation. Let's say you have a folder called 'myfilesdirectory' on your server, in which you upload your text files which you want to use on your website. Also you want those files to be somehow accesible via hyperlinks. Smartest way to do so, with as less as work as possible is to use opendir(), readdir(); and some basic php functions like i'll explain now.

My folder contains of these *.txt files, news.txt, contact.txt, links.txt. I want to read those files to an array, which i will use later for two things. First to dynamicly create hyperlinks of those files, and second to asign $_GET values to them, so that we don't need to get away from index.php page at any time.

So here's the full code

<?php$directory = getcwd();$files=array(); #initialize array$dir = opendir("$directory/myfilesdirectory");while (false !== ($f = readdir($dir))) {	if (eregi("\.txt",$f)){ #if filename matches .txt in the name		$fstristr = substr($f, 0, -4);		array_push($files,"$fstristr"); #push into $files array }}foreach ($files as $link){	echo '<a href="?id='.$link.'">'.ucfirst($link).'</a> ';}echo "<br>";if(in_array($_GET['id'], $files)){	include_once $directory.'/myfilesdirectory/'.$_GET['id'].'.txt';}else {	include_once $directory.'/myfilesdirectory/news.txt';}?>

Now to explain it piece by piece...

1)
$directory= getcwd();
set our directory to the position of our script. There are other ways to do it, but i like this one.

2)
$files = array();
initialize $files array which is now empty, but we will fill it later with some PHP functions

3)
$dir = opendir("$directory/myfilesdirectory");
sets variable $dir to open 'myfilesdirectory' in which i saved my files

4)
while (false !== ($f = readdir($dir)))
is used to

Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.

5)
if (eregi("\.txt",$f)){ #if filename matches .txt in the name		$fstristr = substr($f, 0, -4);		array_push($files,"$fstristr"); #push into $files array }

eregi()
is used so that only files with .txt extensions get $f value

$fstristr = substr($f, 0, -4)
is used to strip those extensions by using substr() to read file until last 4 characters which are .txt ;)

array_push($files, "$fstristr")
now we use array_push to fill that $files array with our files that are read and striped nicely to fit our needs. At this time $files array looks something like this

array ([0] => "content", [1] => "links", [2] => "news") notice how it sorted out array, maybe later i will use some kind of function to sort it more convinient than it is now

Basicly that is the "new" code, the rest of the code is more or less familiar to even newbies in php, but i'll explain it briefly..

6)
foreach ($files as $link){	echo '<a href="?id='.$link.'">'.ucfirst($link).'</a> ';}
foreach is used to dynamicly create links from the $files array (ucfirst is used to capitalize first letters)

and last but not the least

7)
if(in_array($_GET['id'], $files)){	include_once $directory.'/myfilesdirectory/'.$_GET['id'].'.txt';}else {	include_once $directory.'/myfilesdirectory/news.txt';}?>

This script looks weather a $_GET['id'] is in array $files, and if true includes that file. Else is the default file which is going to be loaded while $_GET is not yet set.

The preview of the script is availiable here
And a download is availiable here
Edited by matak (see edit history)

Share this post


Link to post
Share on other sites

Thanks, dude! Like your new avatar, too! You last few php topic have been other ways of using the include() function to get the ?id=links pages. Some creative ways to get around the include() function using arrays! I might use this on my site soon! Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.