Re: [SOLVED] Re: Directory Listing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/8/26 Steve <admin@xxxxxxxxxxxxxxxxx>:
> Ashley Sheridan wrote:
>>
>> On Wed, 2009-08-26 at 09:50 +0100, Tom Chubb wrote:
>>
>>>
>>> 2009/8/26 Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx>:
>>>
>>>>
>>>> On Tue, 2009-08-25 at 17:08 +0100, Tom Chubb wrote:
>>>>
>>>>>
>>>>> I've been playing about more and now I have the following code:
>>>>>
>>>>> <?
>>>>> error_reporting(E_ALL);
>>>>> ini_set('display_errors', true);
>>>>>
>>>>> function getDirectory($path = '.', $ignore = '') {
>>>>>    $dirTree = array ();
>>>>>    $dirTreeTemp = array ();
>>>>>    $fileDate = array ();
>>>>>    $ignore[] = '.';
>>>>>    $ignore[] = '..';
>>>>>    $dh = @opendir($path);
>>>>>    while (false !== ($file = readdir($dh))) {
>>>>>        if (!in_array($file, $ignore)) {
>>>>>            if (!is_dir("$path/$file")) {
>>>>>                $dirTree["$path"][] = $file;
>>>>>                $fileDate["$file"][] = date ("d/m/Y",
>>>>> filemtime("$path/$file"));
>>>>>            } else {
>>>>>                $dirTreeTemp = getDirectory("$path/$file", $ignore);
>>>>>                if (is_array($dirTreeTemp))$dirTree =
>>>>> array_merge($dirTree, $dirTreeTemp, $fileDate);
>>>>>            }
>>>>>        }
>>>>>    }
>>>>>    closedir($dh);
>>>>>    return $dirTree;
>>>>> }
>>>>>
>>>>> $ignore = array('.htaccess', 'Thumbs.db', 'index.php');
>>>>> $dirTree = getDirectory('.', $ignore);
>>>>> getdirectory('.');
>>>>>
>>>>> echo "Gatwick Tender Documents\n";
>>>>>
>>>>> foreach( $dirTree as $key => $folder ){
>>>>>   echo "\n"; //Don't need folders as they're shown with the files
>>>>>   foreach( $folder as $file){
>>>>>       echo str_replace("./", "", $key) . "\t" . $file . "\t\n"; //Pad
>>>>> out with a tab for easy import into excel
>>>>>
>>>>>   }
>>>>> }
>>>>> print_r($dirTree); //Just using this for debugging
>>>>> ?>
>>>>>
>>>>>
>>>>> The output is fine for the paths and filenames but I still can't get
>>>>> the dates showing. It's getting the correct date for some but not all.
>>>>> I did something else earlier and found that all the dates were
>>>>> 01/01/1970 but at least there was a date for every file but can't
>>>>> remember how I go there!
>>>>>
>>>>>
>>>>> Here is a sample output result:
>>>>>
>>>>> Gatwick Tender Documents
>>>>>
>>>>> .     9216_100_REV_V1.0_bound.dwg
>>>>>
>>>>> Tender Docs   BAA Works Terms v1.1 (22.05.08).pdf
>>>>> Tender Docs   Contents of Volumes 1 and 2.pdf
>>>>> Tender Docs   Cover Letter and Instructions.doc
>>>>> Tender Docs   Form of Tender.doc
>>>>>
>>>>> Tender Docs/Health and Safety Questionnaire   NT Baggage Tender
>>>>> Questionaire rev2.xls
>>>>>
>>>>> BAA Works Terms v1.1 (22.05.08).pdf   29/07/2009
>>>>>
>>>>> Contents of Volumes 1 and 2.pdf       29/07/2009
>>>>>
>>>>> Cover Letter and Instructions.doc     29/07/2009
>>>>>
>>>>> Form of Tender.doc    29/07/2009
>>>>>
>>>>> Tender Docs/NTB BH Lighting   3J-B-1 PIR.xls
>>>>> Tender Docs/NTB BH Lighting   3J-B-2B PIR.xls
>>>>> Tender Docs/NTB BH Lighting   3J-B-2R PIR.xls
>>>>> Tender Docs/NTB BH Lighting   3J-B-3R PIR.xls
>>>>> Tender Docs/NTB BH Lighting   3J-D PIR.xls
>>>>> Tender Docs/NTB BH Lighting   4G-G PIR.xls
>>>>> Tender Docs/NTB BH Lighting   4J-B-1B PIR.xls
>>>>> Tender Docs/NTB BH Lighting   4J-B-1R PIR.xls
>>>>> Tender Docs/NTB BH Lighting   4J-B-2B PIR.xls
>>>>> Tender Docs/NTB BH Lighting   4J-B-2R PIR.xls
>>>>> Tender Docs/NTB BH Lighting   4J-B-4 PIR.xls
>>>>> Tender Docs/NTB BH Lighting   5G-G PIR.xls
>>>>>
>>>>>
>>>>> Can anyone shed any light on it?
>>>>> I'm about to admit defeat!
>>>>>
>>>>> Thanks in advance and I'm not being lazy - I really am trying!!! :(
>>>>>
>>>>> Tom
>>>>>
>>>>>
>>>>
>>>> The only time I've ever noticed this problem was on a 32bit system where
>>>> the files were above 2GB each. When the files are that size, none of the
>>>> information functions seem to work correctly, including the filesize,
>>>> date, etc.
>>>>
>>>> Thanks,
>>>> Ash
>>>> http://www.ashleysheridan.co.uk
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>> Cheers Ash,
>>> I read that too, but I was getting the error for over 90% of the files
>>> which I know are generally only a few MB.
>>> I've started from scratch again and come up with something that is
>>> easier to deal with but still having one last problem!
>>>
>>> <?php
>>> error_reporting(E_ALL);
>>> ini_set('display_errors', true);
>>> function directoryToArray($directory, $recursive) {
>>>        $array_items = array();
>>>        if ($handle = opendir($directory)) {
>>>                while (false !== ($file = readdir($handle))) {
>>>                        if ($file !="index.php" && $file != "." && $file
>>> != "..") {
>>>                                if (is_dir($directory. "/" . $file)) {
>>>                                        //For Directories
>>>                                        if($recursive) {
>>>                                                $array_items =
>>> array_merge($array_items,
>>> directoryToArray($directory. "/" . $file, $recursive));
>>>                                        }
>>>                                        $fullfile = $directory . "/" .
>>> $file;
>>>                                        $array_items[] =
>>> preg_replace("/\/\//si", "/", $fullfile);
>>>                                } else {
>>>                                        //For Files
>>>                                        $fullfile = $directory . "/" .
>>> $file;
>>>                                        $array_items[] =
>>> preg_replace("/\/\//si", "/", $fullfile);
>>>                                }
>>>                        }
>>>                }
>>>                closedir($handle);
>>>        }
>>>        return $array_items;
>>> }
>>>
>>> $files = directoryToArray("./Tender", true);
>>>
>>>
>>> //Output to browser
>>> foreach ($files as $file) {
>>>        echo str_replace("./Tender", "", $file) . "\t" . "      " . date
>>> ("d/m/Y",
>>> filemtime($file)) . "\n";
>>> }
>>> //print_r($files);
>>> ?>
>>>
>>>
>>> The output I'm getting is:
>>> /9216_100_REV_V1.0_bound.dwg            05/08/2009
>>> /Tender Docs/BAA Works Terms v1.1 (22.05.08).pdf
>>>  29/07/2009
>>> /Tender Docs/Contents of Volumes 1 and 2.pdf            29/07/2009
>>> /Tender Docs/Cover Letter and Instructions.doc          29/07/2009
>>> /Tender Docs/Form of Tender.doc         29/07/2009
>>> /Tender Docs/Health and Safety Questionnaire/NT Baggage Tender
>>> Questionaire rev2.xls           29/07/2009
>>> /Tender Docs/Health and Safety Questionnaire            14/08/2009
>>> /Tender Docs/NTB BH Lighting/3J-B-1 PIR.xls             13/05/2009
>>> /Tender Docs/NTB BH Lighting/3J-B-2B PIR.xls            13/05/2009
>>> /Tender Docs/NTB BH Lighting/3J-B-2R PIR.xls            13/05/2009
>>> /Tender Docs/NTB BH Lighting/3J-B-3R PIR.xls            13/05/2009
>>> /Tender Docs/NTB BH Lighting/3J-D PIR.xls               13/05/2009
>>>
>>> All I need to do now is get a tab between the filename & the path
>>> I'm thinking I need to explode on the "/" and somehow match the last
>>> value but stumped at the moment!
>>>
>>>
>>> Tom
>>>
>>>
>>
>> You could do a substr on $file:
>>
>> $filename = substr($file, strrpos('/')+1);
>> $directory = substr($file, 0, strlen($file) - strrpos('/'));
>>
>> I've not tested that, so you might need to adjust slightly a character
>> position here or there.
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.u
>
> It might make more sense to use the basename and dirname functions for that.
> Though you will probably still run into the misplaced tabs if your paths
> don't have trailing slashes. At least it's cleaner.
>
> $filename = basename($file);
> $directory = dirname($file);
>
> http://www.php.net/manual/function.basename.php
> http://www.php.net/manual/function.dirname.php
>

Thanks Steve - forgot about those!
I tried using dirname but I lost the "/" on files in the root so I
used this instead:

		$filename = basename($file);
		$path = str_replace ($filename, "", $file);

Working perfectly now. Here is the output which is exactly what I wanted:
(Path [tab] Filename [tab] Date Modified [tab] Filesize)

/Addendum 1/Hoare Lea
Drawings/	SPEC-0208660-013-2009-07-23-MWW-A32revA.pdf	05/08/2009	0.05
MB
/Addendum 1/Hoare Lea
Drawings/	SPEC-0208660-013-2009-07-23-MWW-A33revA.pdf	05/08/2009	0.07
MB
/Addendum 1/Industrial Relations/	Capital IR Policy Updated V2
200509.doc	06/08/2009	0.39 MB
/Addendum 1/Industrial Relations/	M&E Agreement April
2009.doc	06/08/2009	0.31 MB
/Addendum 1/Industrial Relations/	Presentation for Delivery Programmes
May 09.ppt	06/08/2009	0.8 MB
/Addendum 1/Logan dwgs/	9216_014_REV_P03.dwg	19/05/2009	2.63 MB
/Addendum 1/Logan dwgs/	9216_110_REV_V1.0_MH Scope.dwg	31/07/2009	5.64 MB
/Addendum 1/Logan dwgs 1/	9216_014_REV_P03.dwg	19/05/2009	2.63 MB
/Addendum 1/Logan dwgs 1/	9216_110_REV_V1.0_MH Scope.dwg	31/07/2009	5.64 MB
/Addendum 1/M & E Scope/	In and Out of Scope List 010509.ppt	06/08/2009	0.13 MB
/Addendum 1/Manual Handling
Presentations/	Baggage_Handling_Lifting_Devices_for_BAA_20080922.ppt	22/09/2008	3.01
MB
/Addendum 1/Manual Handling Presentations/	Manual Handling systems
options presentation.pdf	11/05/2009	0.86 MB


Thanks for your help everyone.
Code is below if anyone is interested....


<?php
/*
This function will scan all files in the directory and all
sub-directories showing the path, filename and last modified date in
tabular format.
Copy the source files to scan onto the webserver and set the scan
directory when calling the function.
*/

error_reporting(E_ALL);
ini_set('display_errors', true);
function directoryToArray($directory, $recursive) {
	$array_items = array();
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if ($file !="index.php" && $file != "." && $file != "..") {
				if (is_dir($directory. "/" . $file)) {
					//For Directories
					if($recursive) {
						$array_items = array_merge($array_items,
directoryToArray($directory. "/" . $file, $recursive));
					}
					$fullfile = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $fullfile);
				} else {
					//For Files
					$fullfile = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $fullfile);
				}
			}
		}
		closedir($handle);
	}
	return $array_items;
}
//Specify directory to scan and decide whether to scan recursively
$files = directoryToArray("./Tender", true);

//Output to browser and view source to copy/paste into excel
foreach ($files as $file) {
	if (filesize($file) !== 0) {
		$filesize = round(filesize($file)/1048576,2);
		$filename = basename($file);
		$path = str_replace ($filename, "", $file);
		echo str_replace("./Tender", "", $path) . "\t" . strrpos('/', $file)
. $filename . "\t" . date ("d/m/Y", filemtime($file)) . "\t" .
$filesize . " MB\n";
	}
}
?>


-- 
Tom Chubb
tom@xxxxxxxxxxxx | tomchubb@xxxxxxxxx
07912 202846

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux