Jump to content

[SOLVED] Folders and Files


zipp

Recommended Posts

I have been working on a script for a couple days now.  I finally am about to give up on it. I cant seem to get anything about it working. What I am trying to do is get a printout of files and folders similar to this:

 

upload/

---file1

---file2

---file3

 

upload/folder1

---file1

---file2

---file3

 

upload/folder2

---file1

---file2

---file3

 

etc...

 

I have been able to get some output, but it comes out all messed up.  It looks like:

 

---file1

---file2

 

---file2

---file3

 

---file3

 

Any help would be much appreciated.

Link to comment
Share on other sites

Here is the code that I am working with:

<?php
$path  =  "upload/"; 

$dir_handle  =  @opendir($path)  or  die("Unable  to  open  $path"); 
while  ($file  =  readdir($dir_handle))  { 
if($file  ==  "."  ||  $file  ==  ".."  ||  $file  ==  "index.php"  ) 
continue; 
$name = $path .''. $file;

if (!is_dir($name)){
for ($files = 0; $files < count($path); $files++){
echo "<strong>Files</strong>:<br>";
echo  "<a  href=\"$name\">$file</a><br>";
echo "<br>";
$files++;
}
}

//print_r(glob($path . '*', GLOB_ONLYDIR));

if (is_dir($name)){
for ($folders = 0; $folders < count(glob("$path/*", GLOB_ONLYDIR)); $folders++){
echo  "<a  href=\"$name[$folders]\">$file</a><br>";
echo "<br>";
$folders++;
}
}


} 
closedir($dir_handle); 
?>

Link to comment
Share on other sites

I saw a recursive function once in the php documentation for listing directories/files.... I couldn't find it, but I need something similar for my own scripts soon, so I went ahead and coded something....  I didn't comment it, but it's fairly straight forward.

 

<?php

function ListFiles($folder) {
$out = array();
$han = opendir($folder);
$f = (strlen($folder) > 1) ? $folder . '/' : '';
while($it = readdir($han)) {
	if($it == '.' || $it == '..') continue;
	if(!is_dir($f.$it)) {
		$out[] = (strlen($folder) > 0) ? $folder . '/' . $it : $it;
		continue;
	}
	$path = (strlen($folder) > 0) ? $folder . '/' . $it : $it;
	$out = array_merge($out, ListFiles($path));

}
return $out;
}

$out = listfiles('folder');

print_r($out);

/*  This is the output from

tree folder /F /A

in cmd

--------------------

Y:\public_html\corbin\common>tree folder /A /F
Folder PATH listing for volume Web_Files
Volume serial number is E054-226C
Y:\PUBLIC_HTML\CORBIN\COMMON\FOLDER
|   this is a file.txt
|
\---subfolder
        file in subfolder.txt
--------------------

output from the print_r

Array
(
    [0] => folder/New Folder/New Text Document.txt
    [1] => folder/New Text Document.txt
)

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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