Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Reading Files From Directory To Array, And Using $_get To Get Them, Simple way to manage lot's of files
matak
post Apr 16 2007, 08:42 AM
Post #1


Super Member
*********

Group: Members
Posts: 413
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



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 biggrin.gif

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

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)
CODE
$directory= getcwd();
set our directory to the position of our script. There are other ways to do it, but i like this one.

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

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

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

QUOTE
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)
CODE
if (eregi("\.txt",$f)){ #if filename matches .txt in the name
        $fstristr = substr($f, 0, -4);
        array_push($files,"$fstristr"); #push into $files array
}


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

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

CODE
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)
CODE
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)
CODE
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

This post has been edited by matak: May 13 2007, 11:29 PM
Go to the top of the page
 
+Quote Post
Imtay22
post Apr 16 2007, 07:43 PM
Post #2


Super Member
*********

Group: [HOSTED]
Posts: 299
Joined: 27-January 07
From: Winter is cold here.
Member No.: 37,984
Spam Patrol



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!
Go to the top of the page
 
+Quote Post
Blessed
post Apr 17 2007, 06:35 PM
Post #3


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



Greetins

thanx mate for the nice tutorial
it explains me a lot here
lokking forward to see more of you tutorialss


Bye
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Getting List Of Directories And Files Using Php(6)
  2. Change Permission With Php Code(3)
  3. How To Sort Files Of A Directory using Php(11)
  4. Spliting An Array(4)
  5. How To List Files In A Directory + Subdirectory And Then Use Them.(8)
  6. Logging Dowload Files From Your Server Onto A Html File(1)
  7. Displaying Files Of A Directory(2)
  8. Changin My Phpnuke Directory...please Help(13)
  9. Display Random File In A Directory(9)
  10. Files Required?(5)
  11. Help Needed With Directory/file Listing Code Infinite Loop(5)
  12. Writing To Files And Such(3)
  13. More Dynamic ?id=browsing With Php (associative Array)(1)
  14. Forms, Text Files, And Php For A Signature Generator.(1)
  15. [php]simple Flat File Text Manipulator(3)
  1. Unofficial Trap17 Hosted Members Directory(13)
  2. Security Issue Writing Files(1)
  3. Grabt Access To My Protected Files(2)
  4. Using Multiple Selection Array In Table To Order Data(1)
  5. Getting An Array Value Of A Dynamic Variable(9)
  6. I Need Some Proof Reading For My Code Please! [resolved](7)
  7. Php Ftp Upload Form(1)
  8. Remove A Value From A Php Array Based On Its Value(5)
  9. How To Display Images Of A Directory(4)
  10. Download Script For Mp3 Files(0)
  11. Need Some Help In File Browser(8)
  12. Reading Emails Using Php (imap)(0)
  13. Manage Double Posting In Php(4)


 



- Lo-Fi Version Time is now: 11th October 2008 - 04:35 AM