Jump to content


Photo

is_dir() and ch_dir(), oh my!


  • Please log in to reply
3 replies to this topic

#1 aezell

aezell
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 07 June 2006 - 06:14 AM

I am super rusty in PHP after teaching English for two years and I don't think I knew how to do this before. So, please help me learn and understand.

I need to take a directory like: $saveDir = "c:/home/www/temp/"
and then drill down through whatever directories *might* be in it until I get to a directory with *only* files.

I am certain that a directory will contain either 1) another directory or 2) files. The goal eventually is to get down to the files and then copy them to some other place, but drilling down through the possible directories and checking for new directories or files is where I am stumbling.

My pseudocode thinking is this:
foreach ($item) {
  if(is_dir($item)) {
    ch_dir($item);
} else {
    parse_dir($item); // <- not sure how to use this function
  }
}

Something like that, but I honestly don't have much clue past that.

TIA!

This is where I am at now. The course_id and session_id are just used in the directory names, so overlook that for the moment.

function digDirectory ($handleDir,$course_id,$session_id) {
    $permSaveDir = $this->config["htmlRootDir"]."/courses/".$course_id."/".$session_id;
    if ($handle = opendir($handleDir)) {
        while (false !== ($file = readdir($handle))) {
            if (!is_dir($file)) {
                rename($handleDir . "/" . $file,$permSaveDir . "/" $file);
            } else {
                $this->digDirectory($file,$course_id,$session_id);
            }
        }
        closedir($handle);
    }
    return true;
}


#2 venkir

venkir
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 July 2006 - 01:14 AM

I think you are looking for a way to browse the local file system. One way I have done it in the past is something like this:

"<form action=\"saveoutput.php\" method=\"post\" enctype=\"multipart/form-data\">
    <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"5000000\">
    <table border=\"0\">
    <tr>
    <td><b>Please select directory and file name for output:</b></td>
    <td><input type=\"file\" name=\"savefile\"></td>   
    </tr>
    <tr></tr>
    <tr><td><input type=\"submit\" value=\"Save Output to kml format\"></td></tr>
    </table>
</form>";

The <input type="file" ...> wil create a button with the prompt 'Browse..' and that will let you browse the file system and pick a file.

Hope that helps!!

-Venki

#3 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 12 July 2006 - 06:07 AM

Venki -- Seems to be looking for a way to browse the SERVER filesystem..

I am looking in to this one.

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 12 July 2006 - 06:34 AM

<?php
header("Content-type: text/plain");

function parse_dir($dir)
{
	if(substr($dir,-1) != '/')
	{
		$dir .= "/";
	}
	
	$contents = scandir($dir);
	unset($contents[0]);
	unset($contents[1]);
	foreach($contents as $content)
	{
		echo "{$dir}{$content}\n";
		if(is_dir($content))
		{
			parse_dir("{$dir}{$content}/");
		}
	}
}

parse_dir("/var/www/");
?>

You need recursion, this would do it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users