newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?
| From: | Govinda | Date: | Sun, 24 Aug 2008 22:40:15 +0000 |
| Subject: | newbie Q: How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-278605@lists.php.net to get a copy of this message | ||
Hi all :-)
It'll be fun to work here with you guys over the coming months. 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.
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 "..".
Feel free to enlighten me on that, but, always trying to be 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";
$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) {
if (true == ($file="xxxx.jpg")) {
echo "$file<br />";
}
}
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 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).
How to say, "if the fileNAME is equal to...", or better yet, "if the fileNAME ends with '.jpg'"?
Thanks,
-Govinda