Govinda schreef:
Hi all :-)
It'll be fun to work here with you guys over the coming months.

fun? work? you must be new :-)

I have been out of the coding loop for 7 years, and I am totally new to PHP, but I should catch up not-too-slowly as I used to code HTML, WebDNA, and a little Visual Basic too. Kindly bear with me.

little bear or big bear?

okay enough jokes (but now you know the list flavor at least)


First Q:
This code does list the files in my dir just fine:

$ThisDir = getcwd()."/thumbs";
$DirHandle = opendir($ThisDir);
if ($DirHandle = opendir($ThisDir)) {
   echo "Directory handle: $DirHandle\n";
   echo "Files:<br /><hr width=\"25\%\" align=\"left\" />";

while ((false !== ($file = readdir($DirHandle))) & 1) {
            echo "$file<br />";
    }
    closedir($DirHandle);
}

But two of those entries are apparently named "." and "..".

they are quite common in most filesystem :-P

Feel free to enlighten me on that, but, always trying to be

like John Lennon said ... by heck you gotta save yourself
(damn I said I'd stop with the jokes didn't I)

self-sufficient, I attempt to workaround by wrapping this line:
echo "$file<br />";
with an if statement that I want to only show the file if it ends with the extension ".jpg". I haven't yet found the 'string.ends_with' function in the docs, so I am just seeing if I can (to learn step by step) instead wrap that echo line with an if statement that says "if the name of the file is equal to (xxxx.jpg)", like so:

$ThisDir = getcwd()."/thumbs";

becareful with getcwd(), look into other methods of determining
paths, e.g. dirname(__FILE__).

$DirHandle = opendir($ThisDir);
if ($DirHandle = opendir($ThisDir)) {
   echo "Directory handle: $DirHandle\n";
   echo "Files:<br /><hr width=\"25\%\" align=\"left\" />";

save yourself a million slashes:

        echo 'Files:<br /><hr width="25%" align="left" />';

double quotes interpolate variables, single quotes don't.


while ((false !== ($file = readdir($DirHandle))) & 1) {
        if (true == ($file="xxxx.jpg")) {
            echo "$file<br />";

this *could* be written as:

        echo $file, '<br />';

just a thought.

            }
    }
    closedir($DirHandle);
}

But instead of printing only the name of the file which is equal to "xxxx.jpg", it seems to actually set the value of $file to "xxxx.jpg" on

use == to test equality, = is to set a variable.

each iteration of the while loop, so what I get is a list of files all having the same name ("xxxx.jpg") and their number is equal to the actual number of files in that dir (plus 2 - for the "." and ".." files).

there is is_dir() to check if it's a dir. but that's not what you want here.

you could do something hackish like

if (stripos(strrev($file), "gpj.") === 0) {
        echo $file;     
}

note the ===, 3 equals signs here is very important! check the docs for why.

or you could go a little more robust with regular expressions

if (preg_match("#^.*\.(jpe?g|gif|png)$#i", $file) {
        echo $file; // any image file is echo'ed
}

if (preg_match("#^.*\.jpe?g$#i", $file) {
        echo $file; // any jpeg file is echo'ed
}


How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?

http://php.net/manual/en/ref.strings.php lists all the basic string funcs,
have fun with inconsistent argument order in some of those! after 7+ years
of php I still get the order wrong for some of them :-)

Thanks,
-Govinda



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

Reply via email to