Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,624 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,046 people online right now. Registration is fast and FREE... Join Now!




List of Folders

 
Reply to this topicStart new topic

List of Folders, Links of a list of folders

formalsleek
post 25 Nov, 2005 - 10:27 AM
Post #1


New D.I.C Head

*
Joined: 25 Nov, 2005
Posts: 21


My Contributions


Hi, I am writing a review script, i have created a script that adds a review in a new folder. The folder name depends on the name of the review, and the file inside is just an index.php with a template and the review.

My problem is actually creating a list which picks up all the directories and puts it into a list which is linked to each folder. So if there is a review called Anime for instance it would show the name Anime, which would be linked to root/reviews/anime/. I have searched the internet looking for various ways to do this, and i have had no suck luck. I am though certain that it is undoubtedly possible to do.

Could you give me a link or any bits of code that would help? thanks...
User is offlineProfile CardPM

Go to the top of the page

Wizzy
post 25 Nov, 2005 - 04:08 PM
Post #2


D.I.C Regular

Group Icon
Joined: 20 Nov, 2005
Posts: 408



Thanked 1 times

Dream Kudos: 145
My Contributions


REVIEW FORM
addreview.html
CODE
<html>
<head>
<title>Review</title>
<script language="javascript">
function resetmsg() {
var warn = prompt('Are you sure you want to clean out your message?');
if(warn=1) reset(document.forms[0].msg);
}
</script>
</head>
<body>
<form method="post" action="action.php">
<table align="center" style="border: 1px solid #666666">
<tr>
<td colspan=2><h1>Review</h1></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>Review:</td>
<td><textarea name="msg"> </textarea></td>
</tr>
<tr>
<td>Rating:</td>
<td><select>
<option name="one">1</option>
<option name="two">2</option>
<option name="three">3</option>
<option name="four">4</option>
<option name="five">5</option>
</select></td>
</tr>
<td><input type="submit" name="verifyreview"/></td>
<td><input type="button" name="reset" onclick="resetmsg();"</td>
</tr>
</table>
</form>
</body>
</html>


in another script (PHP) you would use the $_POST[] variables to either submit the values to a text file, db, or csv file.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 25 Nov, 2005 - 05:24 PM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Hmm...I'm not sure this addresses the user question.

formalsleek, are you actually seeking a script that traverses a set of directories, storing the names, and then outputs that list (likely stored in an array) to the user...each item in the list being a link to that particular item?

If so, are you using a database? You can store a list of directories in the db, and pull that out for display, or you can traverse the directories themselves, grabbing the names.

Since there appears to be some ambiguity around your question, could you please take a moment to clarify?
User is offlineProfile CardPM

Go to the top of the page

Wizzy
post 25 Nov, 2005 - 05:35 PM
Post #4


D.I.C Regular

Group Icon
Joined: 20 Nov, 2005
Posts: 408



Thanked 1 times

Dream Kudos: 145
My Contributions


here are some directory functions you may find useful

chdir (PHP3 , PHP4 )
change directory

int chdir (string directory)
Changes PHP’s current directory to directory. Returns FALSE if unable to change directory, TRUE
otherwise.

dir (PHP3 , PHP4 )
directory class

new dir (string directory)
A pseudo-object oriented mechanism for reading a directory. The given directory is opened. Two
properties are available once directory has been opened. The handle property can be used with other
directory functions such as readdir(), rewinddir() and closedir(). The path property is set to path the
directory that was opened. Three methods are available: read, rewind and close.

Example 1. Dir() Example
CODE
$d = dir("/etc");
echo "Handle: ".$d->handle."<br>\n";
echo "Path: ".$d->path."<br>\n";
while($entry=$d->read()) {
echo $entry."<br>\n";
}
$d->close();


closedir (PHP3 , PHP4 )
close directory handle

void closedir (int dir_handle)
Closes the directory stream indicated by dir_handle. The stream must have previously been opened by
opendir().

opendir (PHP3 , PHP4 )
open directory handle

234
Directories
int opendir (string path)
Returns a directory handle to be used in subsequent closedir(), readdir(), and rewinddir() calls.

readdir (PHP3 , PHP4 )
read entry from directory handle

string readdir (int dir_handle)
Returns the filename of the next file from the directory. The filenames are not returned in any particular
order.
Example 1. List all files in the current directory
CODE
<?php
$handle=opendir(’.’);
echo "Directory handle: $handle\n";
echo "Files:\n";
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
?>


Note that readdir() will return the . and .. entries. If you don’t want these, simply strip them out:

Example 2. List all files in the current directory and strip out . and ..

CODE
<?php
$handle=opendir(’.’);
while ($file = readdir($handle)) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
?>


rewinddir (PHP3 , PHP4 )
rewind directory handle

void rewinddir (int dir_handle)
Resets the directory stream indicated by dir_handleto the beginning of the directory.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 25 Nov, 2005 - 05:56 PM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Also, here is an excellent article written by one of the team members here at dream in code (cyberscribe == da man). The article itself is about recursion, but to demonstrate the use of recursion, he implements a small file traversal system...it's toward the end of the article...it may well give you some good tips on how you wish to do it.
User is offlineProfile CardPM

Go to the top of the page

formalsleek
post 28 Nov, 2005 - 02:13 AM
Post #6


New D.I.C Head

*
Joined: 25 Nov, 2005
Posts: 21


My Contributions


Yea i undestand that readdir() reads the files in the folder, what i am trying to do is read the name of the folders (inside the review folder), so that i can then make a list for the visitors, listing all the reviews.

an array may work, but i still need to get the names of the folders, to insert them into an array.

CODE


<?
$name=$_POST['name'];
$logo=$_POST['logo'];
$rating=$_POST['rating'];
$review=$_POST['review'];



$newDir = "./" . $name;
$makedir = mkdir($newDir, 0777);
if ($makedir){
$FileName = "$name/index.php";
$fh = fopen($FileName, 'w') or die("can't open file");
$stringData1 = "<b>Name:</b> $name\n<br><b>Review:</b> $review\n";
fwrite($fh, $stringData1) or die("can't write to file");

// It could be done this way, but then that would mean a lot of work in terms of the certain layout features.

$fdurl = "$name/";
$fhlist = fopen("list.php", 'w') or die("can't open file");
$stringData3 = "<br>- <a href='$fdurl'>$name</a>\n";
fwrite($fhlist, $stringData3) or die("can't write to file");
fclose($fhlist);

echo"<a href='list.php'>Review Added!</a>";
}
else
{
echo "Folder not created";
}
?>


Also to whether i am using a DB, no. I am trying to create flat file system (which was not my choice blink.gif).

This post has been edited by formalsleek: 28 Nov, 2005 - 02:32 AM
User is offlineProfile CardPM

Go to the top of the page

woodytherugger
post 1 Jan, 2006 - 07:07 PM
Post #7


New D.I.C Head

*
Joined: 1 Jan, 2006
Posts: 1



Thanked 1 times
My Contributions


formalsleek,
Here is a function that i modified from a comment posted on php.net, http://us2.php.net/opendir, the origional code was to list all directories, sub directories and files in each.

I just wanted a list of folders in my current working directory that linked to the actual folder, which would then show a list of of the folders and files in each provided that there was no index. I used this to quickly navigate all of my current website projects.

CODE

<?php
function folderlist(){
 $startdir = './';
 $ignoredDirectory[] = '.';
 $ignoredDirectory[] = '..';
  if (is_dir($startdir)){
      if ($dh = opendir($startdir)){
          while (($folder = readdir($dh)) !== false){
              if (!(array_search($folder,$ignoredDirectory) > -1)){
                if (filetype($startdir . $folder) == "dir"){
                      $directorylist[$startdir . $folder]['name'] = $folder;
                      $directorylist[$startdir . $folder]['path'] = $startdir;
                  }
              }
          }
          closedir($dh);
      }
  }
return($directorylist);
}

$folders = folderlist();
 foreach ($folders as $folder){
   $path = $folder['path'];
   $name = $folder['name'];

   echo '<a href="'. $path . $name .'">' . $name . '</a><br />';
 }
?>


you could change the last echo to
CODE
echo '<a href="'.$path . $name . '/index.php">' . $name . '</a><br />';
just to make shure that they dont get a listing of the files in the folders if there is not an index. also if there are specific folders that you do not want to be on the list you can add them to the $ignoredDirectory[] array

Hope this helps.

Woody
User is offlineProfile CardPM

Go to the top of the page

Wizzy
post 1 Jan, 2006 - 09:48 PM
Post #8


D.I.C Regular

Group Icon
Joined: 20 Nov, 2005
Posts: 408



Thanked 1 times

Dream Kudos: 145
My Contributions


Since you have no db, you could use a txt file instead of using a db. The text file would only need to echo everything. It could be easily modified as well using fwrite
Here is also a full tutorial on how to write to text files.
According to the rules, I can only help you if you put in some effort, but yet you have no code; so no help till you experiment tongue.gif
User is offlineProfile CardPM

Go to the top of the page

Well-known
post 23 Jun, 2007 - 05:55 PM
Post #9


New D.I.C Head

*
Joined: 23 Jun, 2007
Posts: 1


My Contributions


This is something I needed too.

I wrote few scripts that create galleries in three steps.
First step is to create folder (which is the name of gallery too), and subfolders (large images and thumbnails).
Than user uploads images with resizing them. Original image goes to subfolder for large images and resized into thumbnails folder.
Finally, the script copies index.php file into gallery root folder.

I needed a script that indexes folders (galleries) and makes link form them to actual folder (gallery)

Summer
|--- index.php
|--- LARGE
|-- sun.jpg
|-- heat.jpg
|--- THUMBNAILS
|-- sun.jpg
|-- heat.jpg

<a href="/galleries/Summer/">Summer</a>

I did not try your code yet, but thanks for posting!
User is offlineProfile CardPM

Go to the top of the page

dream2rule
post 27 Jun, 2007 - 12:27 AM
Post #10


New D.I.C Head

*
Joined: 26 Jun, 2007
Posts: 5


My Contributions


QUOTE(woodytherugger @ 1 Jan, 2006 - 08:07 PM) *

formalsleek,
Here is a function that i modified from a comment posted on php.net, http://us2.php.net/opendir, the origional code was to list all directories, sub directories and files in each.

I just wanted a list of folders in my current working directory that linked to the actual folder, which would then show a list of of the folders and files in each provided that there was no index. I used this to quickly navigate all of my current website projects.

CODE

<?php
function folderlist(){
  $startdir = './';
  $ignoredDirectory[] = '.';
  $ignoredDirectory[] = '..';
   if (is_dir($startdir)){
       if ($dh = opendir($startdir)){
           while (($folder = readdir($dh)) !== false){
               if (!(array_search($folder,$ignoredDirectory) > -1)){
                 if (filetype($startdir . $folder) == "dir"){
                       $directorylist[$startdir . $folder]['name'] = $folder;
                       $directorylist[$startdir . $folder]['path'] = $startdir;
                   }
               }
           }
           closedir($dh);
       }
   }
return($directorylist);
}

$folders = folderlist();
  foreach ($folders as $folder){
    $path = $folder['path'];
    $name = $folder['name'];

    echo '<a href="'. $path . $name .'">' . $name . '</a><br />';
  }
?>


you could change the last echo to
CODE
echo '<a href="'.$path . $name . '/index.php">' . $name . '</a><br />';
just to make shure that they dont get a listing of the files in the folders if there is not an index. also if there are specific folders that you do not want to be on the list you can add them to the $ignoredDirectory[] array

Hope this helps.

Woody


Hey Woody, this was a very helpful script. But i would like to display the list of directories and sub-directories in a tree structure format. That is, displaying all the main folders with the folder icon and stuff.
This would be a great help again.

i wan it to be something like:
+ Folder 1
+ Folder 2
+ Folder 3

- Folder 1
|----- Document 1
|----- Document 2
|----- Document 3
|-----
- Folder 2
|----- Document 1
|----- Document 2
|----- Document 3
|-----
+ Folder 3
.
.
.
.
.
.
..
... So on..

Thanks and Regards,
Dream2rule

This post has been edited by dream2rule: 27 Jun, 2007 - 01:03 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 03:24AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month