PHP file_exists returns false but the file does exist
Asked Answered
R

3

6

I have seen several similar questions, but no answer worked in my situation, except that it probably has something to do with permissions.

A PHP script served by Apache tells me unable to open database file.

When I print the path to that file, it returns a valid path, say DBPATH. The file does exist at that location; I gave it and its parent folder 777 rights; I gave them user:user access, where user is the sudoer that all script files belong to. I did the same to the whole htdocs/ folder, just in case.

When I print file_exists(DBPATH), it returns false. Is is most likely a matter of permissions, but I don't know what I should change for PHP to have access rights. I tried apache:apache, too. I cannot su apache (user not available).

My scripts are in htdocs/. DBFILE is somewhere out of it (I tried /tmp/test, all in 777, but no luck either).

No safe_mode, PHP 5.4 freshly installed, CentOS7.

Please someone give me a clue at least to help debug it. Maybe such as: how can I check whether my file will be readable from apache/my php script, without running the script itself? How can I get the name of the user that is used to execute it?

Remarkable answered 15/10, 2015 at 11:23 Comment(4)
can you open the file through cat DBPATH ?Venettavenezia
file_exists does not lie, somewhere something is not right. I guess your DBPATH is not correct. Note that if you are including your file somewhere the current directory is that of the root document where everything is included in.Inseparable
Did you try with the absolute path to your file?Lasonyalasorella
I am printing the absolute path (starts with /home/user/..., as stated by pwd -P), and I can "ls <DBPATH>" and see it exists and has 777 rights. I can open it with sqlite3 from anywhere else.Remarkable
R
3

Solved, more or less.

To debug I had the idea to move DBFILE to the same folder where the PHP script lives, and check it can find it - it did. Then I move DBFILE one folder after another in the tree to see where it stopped finding it.

It occurs that if only one of the folders in the whole path does not have execute rights for all users (xx5), the file cannot be found and file_exists returns false.

So the solution was to create another folder in a totally executable place (/var/www/data/ worked after chmod 755 data), and move the file there.

Remarkable answered 15/10, 2015 at 12:14 Comment(0)
J
0

Do you use an absolute path or relative path? Because file_exists() doesn't work with HTTP addresses (which is an absolute path). But you can enter the relative path. I had the same problem and it fixed it. It was the same problem with unlink().

Exemple:

$file_relative_path = "./wp-content/uploads/fileDirectory/fileName.jpg";  
if (file_exists($file_relative_path)) {  
    unlink($file_relative_path);  
}
Jog answered 20/5, 2021 at 7:47 Comment(0)
E
0

I had a similar problem and was able to solve it by the answer of JulienD: If the execute flag of a directory in the file system (Linux) is not set, then PHP (still) scans this directory with glob or scandir. However, a subsequent check with file_exists() on this list of results, I wanted to find broken symbolic links, returned false! So the solution was to set the Execute right for the directory, as mentioned by JulienD.

Eshelman answered 19/8, 2022 at 7:6 Comment(2)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewJump
You are right, I changed the text according to your comment. As far as I understood, the problem and solution was about the assignment of rights at file levels.Eshelman

© 2022 - 2024 — McMap. All rights reserved.