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

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

 





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


Right. That's linux doing that, not php. If you jump into a ssh connection and do an

$ ls -la

you will see

. and .. at the top.

if ($filename == '.' || $filename == '..') {
  continue;
}

  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).

Because you have:

$file = 'xxx.jpg';

That is an assignment.

What you want is a comparison:

$file == 'xxx.jpg';

Note the double ='s.

http://www.php.net/manual/en/language.operators.php


In this case, I'd use the pathinfo function to break up the file/path:

http://www.php.net/manual/en/function.pathinfo.php


--
Postgresql & php tutorials
http://www.designmagick.com/


--
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