Page 1 of 1

help in php script

Posted: Wed Dec 03, 2008 3:03 pm
by khalidanwar123
i am trying to read a folder and write its file names into excel.
here is script it works for small folder but my folder is in terabyte.
It is not able to open it for reading. is there any way to do so ?

<?php
ini_set("display_errors",1);
$ourDir = "C:/wamp/";
$d = dir($ourDir);
// prepare to read directory contents
$ourDirList = opendir($ourDir);
// loop through the items
$cntr=1;
$cnt = 1;
$ourFileName = "testFile1.csv";
while ($entry = readdir($ourDirList)) {
//if(is_dir($entry)){ // not always work
if(is_dir($ourDir."/".$entry)){ // always work
//echo "directory ".$cntr." is ==> $entry <br />";
if($cnt == 2){
$ourFileName = "testFile".$cntr++.".csv";
$cnt = 1;
}
$ourFileHandle = fopen($ourFileName, 'a+') or die("can't open file");
$data = $entry.",";
fwrite($ourFileHandle,$data);
fclose($ourFileHandle);
$cnt++;
$cntr++;
}
if (is_file($entry)){
// echo "file: $entry <br />";
}
}
$cntr = 0;
closedir($ourDirList);
?>

Re: help in php script

Posted: Thu Dec 04, 2008 6:04 am
by novice4eva
Your code doesn't help much!! Please tell us more details of what you want to achieve, it seems like you are creating csv file for each folder encountered and with conditional check

Code: Select all

$cnt == 2
, are you trying to avoid the first directory encountered ?? :dubious:
khalidanwar123 wrote:here is script it works for small folder
I bet it worked only for the folder but not for files

Re: help in php script

Posted: Thu Dec 04, 2008 11:58 am
by khalidanwar123
There is a folder containing millions of other folders in it,
Thus became a huge one ( in terabytes ) .
I'm trying to read the folder names within that. There is only 2 level directory structure.
One root folder and millions of folders in it.
The above code is an example what i am trying to do.
I do not require "." and ".." which i can put the check in future.
The actual thing i am trying to do is read a chunk of folder names from root folder and
and write it in a csv file.
The above code run successfully if the root folder is small in size.
But now it has become in terabytes , so i am not able to read it.
I want to know how i can open that folder to read its contents ( folder names ).

I hope you might have got the idea.

sorry about my poor English.

Re: help in php script

Posted: Thu Dec 04, 2008 10:16 pm
by novice4eva

Code: Select all

 
$ourDir = "C:/wamp/";
$ourDirList = opendir($ourDir);
$ourFileName = "testFile1.csv";
$ourFileHandle = fopen($ourFileName, 'a+') or die("can't open file");
while ($entry = readdir($ourDirList)) 
{
    if(is_dir($ourDir."/".$entry) && ($entry!='.' || $entry!='..'))
        $data.= $entry.",";
}
fwrite($ourFileHandle,$data);
fclose($ourFileHandle);
closedir($ourDirList);
 
The above code will write the folder names but only the folders inside the wamp directory, it doesn't do recursive test to check if directory exists within directory. Since you were opening and closing files inside the while loop it must have slowed down the stuff a bit. But since things are in terabytes: something i have never encountered, i dunno how things will go with this code...just in case check max_execution_time value in php.ini, if it takes too much time and throws error " Maximum execution time of n seconds exceeded" then try increasing this value. I would stick with c/c++/java for these tasks...

Re: help in php script

Posted: Fri Dec 05, 2008 3:07 am
by khalidanwar123
Thanks i will try this , if things did not work then i will think about c,c++ or java.