开发者

PHP file_exists($var) not working

I'm trying to write some code on my notebook and am using the xampp environment. I have the following piece of code:

class A {
...
  foreach ($blocks as $block) {
    $block = 'dir/dir2/' . $block;
  }
  if (file_exists($block) == true) {
    $var .= file_get_contents($block);
  }
}

When I echo the $block variable in the foreach loop, it gives back the path to the files. However, the file_exists function always returns false. Could you please help me figure out what开发者_开发问答's wrong here?


file_exists purpose is to check if the supplied file exists. It's returning false. This means that the your file doesn't exist where php is looking. php might be looking in a different area than you expect. Looks like it's time for some debugging.

Run this to figure out where php is looking.

echo "current working directory is -> ". getcwd();

Is that where you want php to look? If not then change the directory php is looking in with the chdir function.

$searchdirectory = "c:\path\to\your\directory"; //use unix style paths if necessary
chdir($searchdirectory);

Then run your function (note: I flipped the slashes to backslashes in order to be consistent with windows style paths.)

class A {
...
  //change working directory
  $searchdirectory = "c:\path\to\your\directory"; //use unix style paths if necessary
  chdir($searchdirectory);

  foreach ($blocks as $block) {
    $block = 'dir\dir2\' . $block;

    if (file_exists($block) == true) {
      $var .= file_get_contents($block);
    }
  }
}


Quoting someones comment on the php file_exists manual page,

Be aware: If you pass a relative path to file_exists, it will return false unless the path happens to be relative to the "current PHP dir" (see chdir() ).

In other words, if your path is relative to the current files directory, you should append dirname(__FILE__) to your relative paths, or as of PHP 5.3, you can just use __DIR__.


It may be too late but just found its solution after banging my head for last 2 hrs. In case you are using windows, it hides file name extension by default. So a.txt will be displayed as a and the file which is displayed as a.txt is actually a.txt.txt . To view file extensions go to Control Panel -> Appereances And Personalization -> Folder Option -> View
and uncheck hide file name extension. Now you can see the true name which is to be used in file_exists().


You are mixing slashes, this one drove me nuts.

Here is the solution:

echo getcwd();
$searchimg = getcwd().'\\public\\images\\pics\\'.$value['name'].'.'.$value['ext'];

You will need to quote the slashes like that all the way to the picture :)


As far as I see, you are using file_exists outside the foreach loop. Hence the $block variable is not bound at the time.

EDIT: actually it is still bound to the last value in your collection.


Do you intend to check file_exists() inside the foreach loop?

class A {
  ...
  foreach ($blocks as $block) {
    $block = 'dir/dir2/' . $block;

    // Inside the loop      
    if (file_exists($block) == true) {
      $var .= file_get_contents($block);
    }
  }
}


i guess that the check of file_exists work a bit different from opening with file_get_contents. so i maye it could be one of these problems:

  • file_exists: returns FALSE if inaccessible due to safe mode restrictions
  • file_exists: for checking it uses the real UID/GID and not the effective rights
  • file_get_contents: uses a protocol with file_exists does not know..

maybe it helps you further! good luck


If you use a variable for your file, make sure that there are no blank space at the end. I went mad until i found the solution : removing blank with trim(). See below.

if file_exists(trim($Filename))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜