1

This is madness, hoping someone can explain.

$dir = getcwd();  

$a = "/var/www/vhosts/mysite/httpdocs/sub1";
$b = "/var/www/vhosts/mysite/httpdocs/sub2";

if( ($dir == $a) || ($dir == $b) ){
$dirlist = glob("../images2/spinner/*.jpg");
}else{
$dirlist = glob("images2/spinner/*.jpg");
}

works fine but

$dir = getcwd();  

if( ($dir == "/var/www/vhosts/mysite/httpdocs/sub1") || ($dir == "/var/www/vhosts/mysite/httpdocs/sub2") ){
$dirlist = glob("../images2/spinner/*.jpg");
}else{
$dirlist = glob("images2/spinner/*.jpg");
}

doesn't. (By doesn't work I mean it returns false, I also tried === )

Anyone?

8
  • 4
    This should not be possible. I doubt that that is the literal code you're executing. So it hardly passes for an SSCCE which means it's hard to diagnose what's actually going on here. Commented Aug 2, 2013 at 12:19
  • Not to mention that there must be a better way to solve whatever problem you're trying to solve here. Commented Aug 2, 2013 at 12:20
  • @deceze there is yes, I've now improved the code to move away from this example, the question wasn't is there a better way though was it? Commented Aug 2, 2013 at 12:26
  • 3
    Possible reason for the problem: your cwd doesn't match either of the two locations, your cwd is different in the two test runs, you're running on one of those systems where glob cannot distinguish between an empty match and an error. Better approach: Build an absolute path for glob instead of relying on the current working directory. Starting with dirname(__FILE__) is good. Commented Aug 2, 2013 at 12:30
  • 1
    Please narrow down what the problem is. Is it the if condition or is it the glob? var_dump($a == "/var/www/vhosts/mysite/httpdocs/sub1"); var_dump($dir == "/var/www/vhosts/mysite/httpdocs/sub1"); var_dump($dir == $a); Commented Aug 2, 2013 at 13:15

2 Answers 2

4

Looks like you have run into the if true then this else everything else bug. You made the mistake of assuming that $dir can only be $a or $b which as Luc M stated is not always the case.

We were just talking about this on programmer exchange yesterday.

https://softwareengineering.stackexchange.com/questions/206816/clarification-of-avoid-if-else-advice

Here is an alternative way of handling the logic.

 $base = dirname(__FILE__);
 $path = '/images2/spinner';
 if(file_exists($base.$path))
 {
    $path = $base.$path;
 }
 else if(file_exists($base.'/../'.$path))
 {
    $path = $base.'/../'.$path;
 }
 else
 {
      throw new Exception('Images not found.');
 }
 $dirlist = glob($path.'/*.jpg');

I wouldn't hard code a host path into your logic. That will lend itself to more bugs. Try to use relative paths to the current source file when possible, and if you can't. Place your hard coded paths in a config.php file as constants and include that file.That will store those values in one place.

Sign up to request clarification or add additional context in comments.

6 Comments

-1 using relative paths is not better than hardcoding host paths, as relative paths rely on the set include path, which doesnt even necessarily include the current working directory. A better way is to use __DIR__ constant, or if you have an older php version, dirname(__FILE__)
@griffin yes, I absolutely agree with you, but I have no idea where images2 exists for the person asking the question.
@MathewFoscarini true that. Removed the downvote, but you should still change the suggestion of using relative paths. (suggesting one bad solution to substitute another one when there is a better solution is not a good idea ...)
@MathewFoscarini Works ;) Thanks for taking criticism to improve the answer ;)
@griffin I'm married. I have a thick skin for criticism.
|
3

Verify the value returned by getcwd()

From http://www.php.net/

getcwd

Returns the current working directory on success, or FALSE on failure.

On some Unix variants, getcwd() will return FALSE if any one of the parent directories does not have the readable or search mode set, even if the current directory does. See chmod() for more information on modes and permissions.

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

1 Comment

But the current working directory is always being returned. if getcwd = this/dir and i have a variable a that also = this/dir then why does that work but comparing it to a string of this/dir doesn't? Do you see the madness? the string and the variable are identical. In one loop I'm comparing the dir with the string, in the other with the variable. They shoudl both either be true or false, no middle ground but they don't. One returns true and the other doesn't. If this was because of the getcwd being incorrect...it wouldn't work with either. May as well say does false == true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.