List of Folders

Links of a list of folders

Page 1 of 1

9 Replies - 71029 Views - Last Post: 27 June 2007 - 01:27 AM Rate Topic: -----

#1 formalsleek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-November 05

List of Folders

Posted 25 November 2005 - 11:27 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: List of Folders

#2 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: List of Folders

Posted 25 November 2005 - 05:08 PM

REVIEW FORM
addreview.html
<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.
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: List of Folders

Posted 25 November 2005 - 06:24 PM

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?
Was This Post Helpful? 0
  • +
  • -

#4 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: List of Folders

Posted 25 November 2005 - 06:35 PM

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
$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
<?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 ..

<?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.
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: List of Folders

Posted 25 November 2005 - 06:56 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 formalsleek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-November 05

Re: List of Folders

Posted 28 November 2005 - 03:13 AM

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.


<?
$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:).

This post has been edited by formalsleek: 28 November 2005 - 03:32 AM

Was This Post Helpful? 0
  • +
  • -

#15 woodytherugger  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 01-January 06

Re: List of Folders

Posted 01 January 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.

<?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
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
Was This Post Helpful? 1

#16 Wizzy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 408
  • Joined: 20-November 05

Re: List of Folders

Posted 01 January 2006 - 10:48 PM

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 :P
Was This Post Helpful? 0
  • +
  • -

#17 Well-known  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-June 07

Re: List of Folders

Posted 23 June 2007 - 06:55 PM

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!
Was This Post Helpful? 0
  • +
  • -

#18 dream2rule  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 27-June 07

Re: List of Folders

Posted 27 June 2007 - 01:27 AM

View Postwoodytherugger, on 1 Jan, 2006 - 08:07 PM, said:

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.

<?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
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 June 2007 - 02:03 AM

Was This Post Helpful? 1

Page 1 of 1