phpapachefile-get-contentspermission-deniednetwork-share

file_get_contents() "failed to open stream: Permission denied" in Apache server


Im trying to read a file in a shared network folder which i can reach but php doesnt have access to.

The shared folder "xtern" is located on computer "saturn" and the file is called "seq_ws.csv".

here is my code:

<?php 
$fileURL = "\\saturn\xtern\seq_ws.csv";
$fileAvailable = file_exists($fileURL);

if($fileAvailable){
    echo "Succes";
}else{
    echo "fail";
    echo file_get_contents($fileURL);
}
?>

which results in the following;

fail
Warning: file_get_contents(\saturn\xtern\seq_ws.csv): failed to open stream: No such file or directory in C:\Apache24\htdocs\file.php on line 9

The strange thing is that i can open the path in both a browser and windows explorer just fine and read the file. The shared folder has all permissions for the user I run Apache on aswell as all permissions for "Everyone".

I have tried researching this problem but havent been able to find any solution. I have checked php.ini and allow_url_fopen is set to On. Im pretty sure it is a setting within Apache, PHP or the shared folder somewhere since i can read the file as stated before but just not with my PHP script.

If anybody has any information or suggestion it would be gladly appreciated,

Thanks in advance.


Solution

  • After much more searching of this problem I finally found the answer, the problem with a UNC path is that the Apache service cant get into shared folders on default(maybe its a bug) cause of the way the service is set up. To fix this problem you gotta link Apache to a user profile, I dont know if this is only a problem in Windows.

    To fix fix Apache not being able to access shared folders:

    1. Go to your local services in Windows services.msc
    2. Right-click on the Apache service and select properties.
    3. Click the "Log on" tab
    4. Change the "Log on as:" setting from Local System account to This account and fill in a account name and password

    If you dont want to keep the same account as your Localhost to connect to the shared folder just duplicate the name and password into the textboxes.

    I hope this will help others that encounter the same problem as I did. While researching this i found many people that did and many diffrent comments/answers but nothing worked.