You are here: php » working with files and directories
Working with files and directories
- Written By
- bos
- Submitted At
- 2009-10-31 10:35:59
- Num Views
- 766
- Category
- PHP
|
Hi there, I am using windows with IIS and php, php expert editor, and one of my frustrations is going between windows explorer, IE and whaterver editors I'm using. I wrote the following funtion to drop in any of the directories I'm using. Now I can close win explorer and use only IE when working on my project. Maybe someone else could use it :-) Cut and paste this into a file called index.php and drop into the folders you're using: <?php //read the list of file and folder names into array $folders $folders = (glob("*")); //area to put the table with files print '<div style="position: absolute; width: 632px; height: 552px; z-index: 1; left: 17px; top: 108px" id="layer1">'; print '<h1><div align="left">This is a list of the folders:</div></h1>'; print '<table border="0" cellpadding="4" cellspacing="1" width="50%">';//table //for each object in $folders array foreach ($folders as $folder){ if (is_dir($folder)) //if it is a folder { print '<tr>'; print '<td style="background-color:DarkGoldenRod">This is a folder:</td>'; print '<td style="background-color:DarkGoldenRod">'.'<a href="'.$folder.'">'.$folder.'</a><br>'; print '</td>'; print '</tr>'; }//end if else //if it is a file { print '<tr>'; print '<td>This is a file:</td>'; print '<td>'.'<a href="'.$folder.'">'.$folder.'</a><br>'; print '</td>'; print '</tr>'; }//end else }//end foreach print '</div>'; ?> By bos @ 2009-10-31 10:35:59
|
|
Very cool. Although I use Zend Studio which allows you to FTP directly to the server and work on the files directly, no need for Explorer. I will give it a try however and let you know how I've found it. Seems like a great idea however! Well done. By Profiler @ 2009-10-31 11:29:25
|
|
Here is a bit of an update on the script. This you can save as fold.php You then point your browser to ..../fold.php and it will load the content of the folder. So you don't use the index page anymore. You can add it to any folder you like. It will check in the child folder if the fold.php is present and append/fold.php to the hyperlink. I have added a bit to make up a url for the parent folder, not very elegant, there must be an easier way to skin this cat. I think that it may give different outcomes on different systems/setups. I will add something something to copy the fold.php into child folders so you don't have to. If not, it will inform you. I hope it's okay forme to post this here. Please let me know if I should stop... Here it is: <?php /*============================================================================*\ fold.php This script will read the contents of a directory and display each item as a hyperlink. For folders the script will check if the same script is present and if so it will display a linnk to the fold.php script. If not, it will display a link to the folder. This is part of a larger/funner project :-) 3 October 09 JD Snyman \*============================================================================*/ //style for header $head = '<head><style type="text/css"> h1 {color:black} td1 {background-color:DarkKhaki} p {margin-left:20px} body {background-color:olive} </style> </head>'; //print header print $head; /*==============This section is for a url to the parent folder ===============*/ $docRoot = getenv("DOCUMENT_ROOT");// $currentDir = getcwd();// $revStr = strrev($currentDir); $parFolder = substr($currentDir, -strlen($currentDir), strlen($currentDir)- strlen(substr($revStr, 0, strpos($revStr, '\\')))-1); if (strlen($docRoot)-1 == strlen($parFolder)) //strlen($docRoot)-1 for the trailing forwardslash { $parUrl = 'http:'.'//'.$_SERVER['SERVER_NAME'].'/'; print "The parent is the root directory: "; print '<td style="background-color:DarkGoldenRod">'.'<a href="'.$parUrl.'/fold.php'.'">'."Parent Directory".'</a><br>'; } elseif (strlen($docRoot)-1 > strlen($parFolder)) { print "This is the root directory: "; $parUrl = 'http:'.'//'.$_SERVER['SERVER_NAME']; print '<td style="background-color:DarkGoldenRod">'.'<a href="'.$parUrl.'/fold.php'.'">'."Parent Directory".'</a><br>'; } elseif (strlen($docRoot)-1 < strlen($parFolder)) { //print "<br>"; //print $parFolder; //print "<br>---"; $revFol = strrev($parFolder); //reverse the folder name $parUrl = substr($revFol,-strlen($parFolder), strlen($parFolder)-strlen($docRoot));//chop of the $docRoot part $parUrl = strrev($parUrl); //turn it back $parUrl = 'http:'.'//'.$_SERVER['SERVER_NAME'].'/'.$parUrl; //ad the server namer to the url print '<td style="background-color:DarkGoldenRod">'.'<a href="'.$parUrl.'/fold.php'.'">'."Parent Directory".'</a><br>'; //print url } /*==========================================Now for the rest =================*/ //read the list of file and folder names into array $folders $folders = (glob("*")); //area to put the table with files print '<div style="position: absolute; width: 632px; height: 552px; z-index: 1; left: 17px; top: 108px" id="layer1">'; print '<h1><div align="left">This is a list of the files and folders:</div></h1>'; print '<table border="0" cellpadding="4" cellspacing="1" width="50%">';//table //for each object in $folders array foreach ($folders as $folder){ if (is_dir($folder)) //if it is a folder { if (file_exists($folder.'/'.'fold.php')) //check if the fold.php file is present in the folder { print '<tr>'; print '<td style="background-color:DarkGoldenRod">This is a folder:</td>'; print '<td style="background-color:DarkGoldenRod">'.'<a href="'.$folder.'/'.'fold.php'.'">'.$folder.'</a><br>'; print '</td>'; print '</tr>'; }//end if else //there is no fold.php file presesnt { print '<tr>'; print '<td style="background-color:DarkGoldenRod">This is a folder:</td>'; print '<td style="background-color:DarkGoldenRod">'.'<a href="'.$folder.'/'.'">'.$folder.' --- No fold.php file present'.'</a><br>'; print '</td>'; print '</tr>'; }//end else }//end if else //if it is a file { print '<tr>'; print '<td>This is a file:</td>'; print '<td>'.'<a href="'.$folder.'">'.$folder.'</a><br>'; print '</td>'; print '</tr>'; }//end else }//end foreach print '</div>'; ?> By bos @ 2009-11-03 10:29:48
|
|
This is great stuff!!! Although if you copy it in all folders that would kinda get messy later on. Why don't you change it to run from the root of the project. Then you could just pass the path you are currently in to the script, not needing the file in all directories? I will test it tonight and let you know what I think. If you want to post full files. Just attach it like this... By PHPin24 @ 2009-11-03 17:23:21
|
|
Haha! I new I was going to have to figure that out sooner rather than later :-) Attached is a later version of the file. You may get an idea of where I'm going with this... I will attach the new one as soon as I have figured out how to do it all from the root. Hopefully before I go to bed tonight ;-) By bos @ 2009-11-03 19:25:56
|
|
Here it is... Could be cleaner I'm sure however, it works :-) Just noticed a couple of errors that I'll work on. (Don't click on the files url :-) "Refresh" also ads complications. (I'll have to add some code to stop that.) I've learned loads though and that was the mission :-) By bos @ 2009-11-06 11:04:42
|
