Jump to content

Odd behaviour with is_readable() and include()


Grooper

Recommended Posts

Hi all,

In short:
My PHP CLI is unable to open files using file_get_contents() but will happily open them with include().

Long version:
If I call is_readable() on any files it returns false, calling file_get_contents() on the same files also returns false, but I am able to include() the files.

The files are ones I have created, but also applies to files created with PHP itself.

The whole thing is hosted on a Synology NAS (for my convenience), I am running PHP CLI 8.x. If I run the same code files hosted on the local machine it runs just fine.

Clearly there is something about the NAS causing some odd behaviour, but what could be causing it? How could PHP open files with include() but not file_get_contents()?

Link to comment
Share on other sites

since you have provided no actual information about paths in use, i'll guess this has to do with relative paths, which include() will search the include_path to find. to get file_get_contents() to search the include path, add the FILE_USE_INCLUDE_PATH flag as the 2nd call-time parameter.

Link to comment
Share on other sites

12 hours ago, mac_gyver said:

since you have provided no actual information about paths in use, i'll guess this has to do with relative paths, which include() will search the include_path to find. to get file_get_contents() to search the include path, add the FILE_USE_INCLUDE_PATH flag as the 2nd call-time parameter.

Thank's for the pointer, I'm not sure that's what's happening though:

I made a simple test script:
 

// Create a new file and put some text in it.
$newFile = "new.txt";
@unlink($newFile);
file_put_contents($newFile, "random text");

// Array of files to look at
$files = array("existing file.txt", $newFile);

foreach ($files as $thisFile) {
	echo "$thisFile:\n";

	var_dump(is_readable($thisFile));
	var_dump(file_get_contents($thisFile))."\n";
	require($thisFile);

	echo "\n\n";
	}

Here I test a file I created in Windows, as well as a file created by the script itself, the output is below:
 

existing file.txt:
bool(false)
string(36) "some random text I typed in earlier."
some random text I typed in earlier.

new.txt:
bool(false)
string(11) "random text"
random text

Interestingly file_get_contents() is now able to open the files, I must have been doing something wrong previously. But still, is_readable() returns FALSE, but PHP proceeds to be able to read the files anyway. The above test results are from the mapped drive on the NAS, running the same script locally gives:
 

existing file.txt:
bool(true)
string(36) "some random text I typed in earlier."
some random text I typed in earlier.

new.txt:
bool(true)
string(11) "random text"
random text

here is_readable() returns TRUE.

Again, this behaviour is curious to me, and is a problem because I was hoping to use a framework that calls is_readable() as a part of it's sanity check during opening files, and it means I can't have the convenience of having my dev files hosted on the NAS.

I would be interested in anyone's input, presumably I am missing something trivial, but I am happy to run any tests suggested. For example, I thought it is likely to be a permissions issue, which would make it an issue with the NAS rather than with PHP, but the above script outputs make me wonder.

Link to comment
Share on other sites

I can tell you that is_readable works just fine for me when accessing files on a mapped network drive.  The drive in my case is mapped to a share provided by another windows 10 machine.  I'd suspect your problem is related to either permissions or the NAS's software.

Link to comment
Share on other sites

7 hours ago, kicken said:

I can tell you that is_readable works just fine for me when accessing files on a mapped network drive.  The drive in my case is mapped to a share provided by another windows 10 machine.  I'd suspect your problem is related to either permissions or the NAS's software.

No doubt you are right, I was being hopeful that is would be something simple with PHP!

Still, I thank everyone for their input!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.