I don’t get the PHP filesystem functions… I got some help yesterday here,
this is a follow-up…
am getting very frustrated b/c I don’t have internet access during the day (this is a personal project) and so can’t search for stuff like I need to… I did manage to download docs so I have a local version of docs, but of course you can’t search it (can’t search for things like “get length of array”, for example…)
first and foremost need to:
- list ONLY directories in dir I’m opening
- filter out parent and g’parent dirs, i.e., any ‘upstream’ dirs… how do I do that? in Java ‘upstream’ dirs are never listed… was not expecting this… need to filter them out of list of files in dir…
wanted to attach screenshot of dir I’m working with… don’t see how to attach files… at any rate, contains “page1”… thru “page6” plus a file created by the mac all over the place, called .DS_store, plus a file called test.txt, that I included for testing purposes…
so from docs:
if (is_dir($dirPgs)) {
if ($dh = opendir($dirPgs)) {
while (($file = readdir($dh)) !== false) { // this just means if dir is not empty, right?
if (is_dir($file)) echo $file . ' -- this file is a dir<br>';
}
}
}
this only prints:
. -- this file is a dir
.. -- this file is a dir
i.e., it only reads the two upstream dirs as dirs!!! it doesn’t recognize “page1”, “page2” etc… as dirs… why on earth is this…
- function ‘glob()’, that someone suggested I use yesterday in other thread, is not getting me anywhere…
it says in docs that you don’t need to use opendir() if you use this function… so how do you access the dir you want?
in example in docs for glob() fn
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
it doesn’t show you how to access dir…
(& what is diff betw using this and using standard
if ( strpos($file, 'page') == true) to filter out file-names?)
I didn’t know how else to access dir, so I did:
if (is_dir($dirPgs)) {
if ($dh = opendir($dirPgs)) {
foreach (glob("page*") as $filename) {
echo 'this is a page dir<br>';
}
}
it prints nothing…
still don’t get why this doesn’t work…
if ( strpos($file, 'page') == true) echo $file . ' -- this is a page dir<br>';`
it prints nothing… while this…
if ( strpos($file, 'age') == true) echo $file . ' -- this is a page dir<br>';
prints:
page1 -- this is a page dir
page2 -- this is a page dir
page3 -- this is a page dir
page4 -- this is a page dir
page5 -- this is a page dir
page6 -- this is a page dir
this doesn’t make any sense…
this also prints nothing:
if ( strpos($file, 'p') == true) echo $file . ' -- this is a page dir<br>';
I don’t get this… why doesn’t it read the 1st char in file name?
also looked @ fn file()…
( array file ( string $filename);
)
so I did:
$arrayOfFiles file( $dirPgs );
this shows a PHP syntax error in my IED…
now the examples for this file() fn only show how to get contents of a file (how many lines in a file, size of file, etc…) can you not use this file() fn to list files in a dir?
man, this is so simple in Java… I never had a hard time in Java with this…
here’s Java code for what I want to do in PHP:
File dirPgs = new File(FPathPgs);
File[] pagesDirs = dirPgs.listFiles();
for (int i=0; i < pagesDirs.length; i++) {
if (pagesDirs[i].isDirectory() ) {
if (pagesDirs[i].getName().indexOf("page") != -1) {
vPages.addElement(pagesDirs[i].getName());
// (use vector since you cannot add elements to arrays, later convert to array)
}
}
}
etc… it seems so simple in Java…
would very much appreciate some help… thank you…