4

I was wondering why I can't check if file exists on parent directory (not from root and not from upper directories)

if(file_exists('../1.txt'))
{
    echo 'File Exists!';
}
else
{
    echo 'File Is Not Exists!';
}

Or is it impossible to check if file exists on parent directory ?

pnuts
  • 58,317
  • 11
  • 87
  • 139
davidmarko
  • 343
  • 3
  • 17
  • 1
    Do you have permission to look in that directory? I don't see why this shouldn't work. What is the result exactly? Do you get an error, or it executes the else-branch when the file exists? –  Jan 22 '15 at 21:42
  • it's entirely possible. checking in the parent directory is no different than checking in a child directory or a sibling directory - it's all just a path. – Marc B Jan 22 '15 at 21:45
  • I am using xampp and I was checking from 2 different locations (root and upper directory) and both does not working... – davidmarko Jan 22 '15 at 21:46
  • @davidmarko Works all fine for me! Please show us your file structure where all is located and which file you want to check – Rizier123 Jan 22 '15 at 21:51

1 Answers1

0

Today I ran into the same problem. I had a MVC handler within the folder

(app root)/mvc(/controller.php)

and wanted to go one folder up and then into the data folder to open an xml. My path was: "../data/foo.xml". It didn' work.

But then it came to me: Since I include the mvc/controller.php from the index.php in the app root, the controller "doesn't know" that it is located as an include in the root folder. And from there it simply has to be "data/foo.xml".

I'm not sure if this is your solution or if this is what you meant with "I was checking from 2 diff. locations", but I believe strongly, that for some devs who look up the problem's effect it could be the pebble to the solution.

Michael
  • 621
  • 5
  • 17