I am trying to write a file in a specific folder but it gives me permission denied
.
So, I verified the permissions and everything seemed all right. Because this is in a development environment, I decided to change the permissions to be "Every one can do anything" to the "root" folder where all my files are written. I tried to replace all the permissions underneath. I tried to remove the inherited permissions. Nothing does it.
I then tried to write a file in the "root" folder. It worked.On the subfolder, it worked. On the subsubfolder. It worked. In the subfolder chain there is a point when it doesn't work, but before the real subfolder.
Here is the path hierachy when it crashes.
$f = new \wee\common\IO\File();
$f->write("DRIVE:/BASE_PATH/files/-/00.jpg", "hello"); // WORKS
$f->write("DRIVE:/BASE_PATH/files/-/mod/00.jpg", "hello"); // WORKS
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/00.jpg", "hello"); // WORKS
$pathLength = strlen("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/00.jpg"); // Real path length is 85
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/00.jpg", "hello"); // FAILS
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/WorkersManager/00.jpg", "hello");
$f->write("DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/WorkersManager/workers/00.jpg", "hello");
The class \wee\common\IO\File
is my creation, but works at a lot of other places.
The exact error message I am getting is:
fopen(DRIVE:/BASE_PATH/files/-/mod/com.ci.company/site/00.jpg): failed to open stream: Permission denied
Just to be 100% clear: The "root" folder is DRIVE:/BASE_PATH/files/
Here is the implementation of the write
method of the File
class.
public function write($fileName, $data, $lock = false) {
$this->_write($fileName, $data, $lock);
}
private function _write($fileName, $data, $lock = false, $openMode = 'w') {
if ($data === null)
$data = "";
$fh = fopen($fileName, $openMode) or die("can't open file"); // FAILS HERE
if ($lock) {
flock($fh, LOCK_EX);
}
fwrite($fh, (is_array($data) ? join("\n", $data) : $data));
if ($lock) {
fflush($fh);
flock($fh, LOCK_UN);
}
fclose($fh);
}
The issue was coming from PHP 5.6.26. Using PHP 5.6.40 fixed it.
I reset to my original permissions and everything is fine!