4
if(file_exists("./squadra/photos/photog.jpg")) {
    echo "### YES ###";
} else {
    echo "### NO ###";
}

if i run this function on /zones/team.php it works (it print YES). If i run this function on /auth/ajax.php it print NO. Why?

EDIT

So i make some experiment.

1 - If i try :

// file on /zones/team.php
if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}

// file on /auth/ajax.php
if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}    

it says NO on both;

2 - If i try :

// file on /zones/team.php
if(file_exists("./squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}


// file on /auth/ajax.php
if(file_exists("../squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}

it says YES on both; But on team.php im using ./ and on ajax.php ../ ...why this works???

David
  • 208,112
  • 36
  • 198
  • 279
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • Are you 10000% sure that is the correct *absolute* path? You have a *root folder* named `squadra`? – Pekka Nov 12 '10 at 21:43
  • im sure, in fact if i write i see the image – markzzz Nov 12 '10 at 21:44
  • Look at @Mikhail's answer below. He's absolutely right. Also make sure you aren't trying to use a web path. The path on the web and the path on the server are not the same thing. – Cfreak Nov 12 '10 at 21:44
  • @markzz and where is the `.` in your original code? – Pekka Nov 12 '10 at 21:45
  • @markzzz: your image path has a "./" at the beginning. "." indicates the current directory. Often you can get the full server path with $_SERVER['DOCUMENT_ROOT'] – Cfreak Nov 12 '10 at 21:46
  • @markzzz: There's a difference between the web root and the file system root. Web root is often `/var/www/mydomain.com/` (or something like that - it's dependent on the system) where file root is just `/`. This is a very big difference. @Cfreaks comment above should help you nail the right path. – Surreal Dreams Nov 12 '10 at 21:53
  • yes i forgot the main dot at the begin of the string. Try to see the new edited topic, maybe is a bit more clear :) – markzzz Nov 12 '10 at 22:00
  • tried all suggestions. Still doesnt works. Try to see the 2 example added at the main topic :) Thanks for your time – markzzz Nov 12 '10 at 22:39

7 Answers7

3

Your last one works most probably because:

  1. You are calling zones/team.php from an index.php which resides in root. In this case ./ part correctly identifies your current directory.
  2. And for ajax, you must be calling it directly like auth/ajax.php, instead of something like index.php?type=jx&do=auth/ajax which would be the same as No.1. Hence this is not the case, you need to get out of auth first with ../, and then go on with squadra/....

Use absolute paths as often as you can. Relative paths are a pain for PHP to calculate them (in performance-wise).

Halil Özgür
  • 15,731
  • 6
  • 49
  • 56
  • yeah i thought was a problem by using an ajax calling. So, i use those function :) in any case, what is strange is that if i use $_SERVER['DOCUMENT_ROOT'] on both it doesnt work :) Thanks for the explanation! – markzzz Nov 13 '10 at 13:12
2

Make sure you are considering the folder that you have typed. You start the file address with / which is server side root. If you want local directory, either remove the preceeding / or type out the entire path.

Secondly make sure you have no typos.

Good luck!

Mikhail
  • 8,692
  • 8
  • 56
  • 82
2

As you got a forward slash, file_exist will go to the root of the HDD.

Use $_SERVER['DOCUMENT_ROOT'] in front of it or remove the slash and use ../, etc, etc.

PHPology
  • 1,017
  • 6
  • 12
1

If squadra is a directory under the directory where the PHP script is running, try

if(file_exists('./squadra/photos/photog.jpg')) {
    echo "### YES ###";
} else {
    echo "### NO ###";
} 
tcrosley
  • 779
  • 3
  • 13
  • yes, this work if i run this function on (for example) /zones/team.php. If i run this function on /auth/ajax.php it doesnt work. How is possible???? – markzzz Nov 12 '10 at 21:58
  • If you are running it from /zones/team.php and it works, this means the image is actually under /zones, e.g. /zones/squadra/photos/photog.jpg. So to run it anywhere in your directory structure, you need to prefix $_SERVER['DOCUMENT_ROOT'] to the root of it, e.g. $_SERVER['DOCUMENT_ROOT'] . "/zones/squadra/photos/photog.jpg" – tcrosley Nov 12 '10 at 22:02
  • uhm no. the image is on /squadra/photos, that's strange :) – markzzz Nov 12 '10 at 22:04
  • try $_SERVER['DOCUMENT_ROOT'] . "/squadra/photos/photog.jpg" then (as suggested in another answer) – tcrosley Nov 12 '10 at 22:06
  • and using $_SERVER['DOCUMENT_ROOT'] . "/zones/squadra/photos/photog.jpg" what happens? It sounds like you may have some strange symbolic links messing things up. Is squadra a real directory or a link to somewhere else? – tcrosley Nov 12 '10 at 22:09
  • uhm, if i try "../squadra/photos/photog.jpg" it works on /auth/ajax.php too. Haha its a bit strange, isnt it? – markzzz Nov 12 '10 at 22:10
  • the two dots (..) means you are going up one directory to your www root. So that would work in any subdirectory one level below (zones, or auth). But it would not work in a PHP page in your www root directory. – tcrosley Nov 12 '10 at 22:12
  • $_SERVER['DOCUMENT_ROOT'] . "/squadra/photos/photog.jpg" should work then from anywhere -- make sure it is typed exactly as shown -- there is no dot right before the first slash "/squadra, instead there is a dot between the ] and " as shown. – tcrosley Nov 12 '10 at 22:14
  • yes! but is so strange that "./string_path" works from /zones/team.php and not from /auth/ajax.php, no? – markzzz Nov 12 '10 at 22:15
  • big difference between . and .. -- one dot ./ references the current directory, two dots ../ references the parent directory – tcrosley Nov 12 '10 at 22:16
  • no this doesnt work. It print always NO if i write exactly as shown... :O – markzzz Nov 12 '10 at 22:17
  • print out the www root with: echo $_SERVER['DOCUMENT_ROOT']; and see what it says – tcrosley Nov 12 '10 at 22:18
  • it print the same folder from each files (team.php and ajax.php) : C:/Program Files/Apache Software Foundation/Apache2.2/htdocs – markzzz Nov 12 '10 at 22:20
  • P.S. on /auth/ajax.php i call that function trought an ajax call, but i don't think this make the difference... – markzzz Nov 12 '10 at 22:21
  • wait : i wrong. If i write $_SERVER['DOCUMENT_ROOT']."/squadra/photos/photog.jpg" it said NO in both team.php, ajax.php – markzzz Nov 12 '10 at 22:26
  • sounds like you have an alias directory set up in your Apache, such that C:/Program Files/Apache Software Foundation/Apache2.2/htdocs is not really the root of your web pages. When you edit your page /zones/team.php for example, what is the full path to the file? (should be able to see that with a Save As command) – tcrosley Nov 12 '10 at 22:33
  • its the same for both directory :) And is that as discussed before C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/zones/team.php – markzzz Nov 12 '10 at 22:37
  • if "../squadra/photos/photog.jpg" works for both as you said earlier then go with that. Don't know why $_SERVER['DOCUMENT_ROOT']."/squadra/photos/photog.jpg" doesn't work. Should be going to the same place. – tcrosley Nov 12 '10 at 22:43
  • No. "../squadra/photos/photog.jpg" works only on /auth/ajax.php (not on team.php)! "./squadra/photos/photog.jpg" works only on /zones/team.php (not on ajax.php)! as works i mean they print YES. IT IS REALLY but REALLY strange... – markzzz Nov 12 '10 at 22:56
1

Check the php safe_mode status, and check the case sensitivity of the file path.

php file_exists

Warning : This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

Mohammad
  • 111
  • 5
0

If you use a relative path with file_exists it returns false unless the path is relative to the php directory.

radicalpi
  • 907
  • 1
  • 9
  • 29
0

Check for the path again - I think that the leading slash is a mistake - as it may point to the root (either server or more likely user space) - while your executing script may be located in a sub-path...

tl;dr; Try removing the first "/"

Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34