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?

Armali
  • 18,255
  • 14
  • 57
  • 171
JulienD
  • 7,102
  • 9
  • 50
  • 84
  • can you open the file through `cat DBPATH` ? – Harshit Oct 15 '15 at 11:27
  • 1
    `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. – x13 Oct 15 '15 at 11:28
  • Did you try with the absolute path to your file? – Julio Soares Oct 15 '15 at 11:30
  • I am printing the absolute path (starts with /home/user/..., as stated by `pwd -P`), and I can "ls " and see it exists and has 777 rights. I can open it with sqlite3 from anywhere else. – JulienD Oct 15 '15 at 11:41

3 Answers3

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.

JulienD
  • 7,102
  • 9
  • 50
  • 84
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);  
}
TomGT
  • 1
  • 1
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.

LElsner
  • 1
  • 2
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32522123) – Webdeveloper_Jelle Aug 24 '22 at 13:33
  • 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. – LElsner Aug 26 '22 at 12:17