Here's my setup. I've got a linux box running Apache and PHP. I have a folder mounted on my linux box that points to a directory on my windows server where my HTML files are stored. My virtualhosts file points to the mounted folder, so Apache is getting the files from the Windows share.
I'm having some issues with fopen. When I run this:
<?php
echo 'cwd is: ', getcwd(), "<br />\n";
echo 'target should be: ', getcwd(), "/data.txt <br />\n";
echo 'file already exists: ', file_exists('data.txt') ? 'yes':'no', "<br />\n";
$file = fopen("data.txt", "a");
if ( !$file ) {
die('fopen failed');
}
$c = fwrite($file, "\n$name,$lastname,$email");
fclose($file);
echo $c, ' bytes written';
?>
I get the following back:
cwd is: /home/brian/website/googlecalendar/html
target should be: /home/brian/website/googlecalendar/html/data.txt
file already exists: no
Warning: fopen(data.txt): failed to open stream: Permission denied in /home/brian/website/googlecalendar/html/testfopen.php on line 7 fopen failed
On Windows, I've checked the permissions on the "html" folder, and everything is set to allow everyone to write. I'm not sure what else I need to change. It should work, right?
Hi
Try,Change the sub directory and files to be in the group "www"
Related
i have a config file and i have to in multiple programs so it's located in /etc
the name of the file is cnf.json and permissions are 666 (read and write for owner, group, others) and the code i have is
try{
$file = fopen(CNF_PATH, "r");
print_r(error_get_last());
$data = fread($file, filesize(CNF_PATH));
echo $data;
}catch(Exception $e) {
}
error is Failed to open stream: Permission denied
what am i doing wrong?
i appreciate your suggestion and solutions.
i'm trying to remove a file using unlink but it return false.
At this point I can't get to the php.ini so as far as I know I can't see the error message. Or can I?
These are the lines I have for now:
echo chmod("MyFolder/testfile.jpg", 0777) ? "chmod Success " : "chmod Fail ";
echo unlink("MyFolder/testfile.jpg") ? "unlink Success" : "unlink Fail";
So the results for running this is chmod Success unlink Fail
echo is_readable("MyFolder/testfile.jpg") ? "Readable " : "Not readable ";
echo is_writable("MyFolder/testfile.jpg") ? "writable " : "Not writable ";
echo is_executable("MyFolder/testfile.jpg") ? "executable " : "Not executable ";
Running these lines I get Readable Writable Not executeable
I don't know what else to do
Edit:
Now when I can see errors and warnings I get this:
Warning: unlink(MyFolder/testfile.jpg): Permission denied in xxx.php on line 19
Edit2:
Tried
$handle = fopen("MyFolder/testfile.jpg", "r");
fclose($handle);
unlink
but still Permission denied
Then I tried changing the working directory like this:
$old = getcwd();
chdir("MyFolder");
unlink("testfile.jpg");
chdir($old);
but same warning; Permisson denied
Edit3:
Tried unlink(realpath("IPCAM/testfile.jpg"));
same warning; Permisson denied
I run my webserver on windows (XAMPP Control Panel v3.2.2). I am creating files and folders like this:
use File;
...
$Path = app_path()
. '/Projects/'
. strtolower($Project->name)
. '/Schema/';
if (File::exists($Path)) {
$result = 1;
} else {
$result = File::makeDirectory($unitPath , '775', true);
$content = "the content is basically a whole controller";
$file = $Path . strtolower($Unit->name) . '.php';
$bytes_written = File::put($file, $content);
if ($bytes_written === false) {
return false;
}
}
The file which I create is a PHP controller.
The files and folders are created. However by using git bash I noticed that the permission is not set to 775 even though I have specified it in makeDirectory, same for the files.
If I try to execute an action from the created file (which is a controller), then I get:
ErrorException in
C:\xampp\htdocs\selenium\vendor\composer\ClassLoader.php line 440:
include(C:\xampp\htdocs\selenium): failed to open stream: Permission
denied
How can I solve this, other than running my webserver on linux instead of windows?
NOTE: Everything works fine on my computer at home, where I also run the webserver on windows. The only difference is that I am using Windows 10 64bit at home and Windows 7 64bit here at work.
UPDATE:
The ErrorException include(C:\xampp\htdocs\selenium): failed to open stream: Permission denied dissapeared after executing composer update. But the question how I can create a file/folders with specific permissions persists.
UPDATE 2:
After creating a new file the ErrorException appears again...
I tried creating a trace file with fopen("name","a") and writing to it, but although the fopen succeeds, and returns a handle, and all of the fwrite calls succeed, I cannot find the file at the location specified by the file name parameter to fopen.
$trace = fopen("/var/tmp/billing.log", "a");
if ($trace)
echo "opened /var/tmp/billing.log<br>\n";
else
echo "unable to open /var/tmp/billing.log<br>\n";
...
fwrite($trace, __FILE__ . ': ' . __LINE__ . "ce(" .
"\$aid=$aid, " .
"\$level=$level, " .
"\$id=$id, " .
"\$mode=$mode, " .
"\$nocache=$nocache)\n");
echo __FILE__ . ': ' . __LINE__ . "ce(" .
"\$aid=$aid, " .
"\$level=$level, " .
"\$id=$id, " .
"\$mode=$mode, " .
"\$nocache=$nocache)<br>\n";
The echo indicating the handle has been returned displays, as do the echos immediately after each call to fwrite, but the requested file /var/tmp/billing.log does not exist. As usual /var/tmp is:
drwxrwxrwt. 35 root root 4096 Jul 15 20:06 tmp
so writing to the directory is permitted. I tested this by manually creating a file by:
cat > /var/tmp/test
This appears to be a poorly documented or explained feature of PHP on systems using systemd. Instead of creating the file where I specified PHP instead creates the file a further layer down in a private directory!
$ cd /var/tmp
$ sudo find -name *.log
[sudo] password for jcobban:
./systemd-private-DGOkBT/tmp/recur-billing.log
./systemd-private-DGOkBT/tmp/process-registration.log
./systemd-private-uEF6lO/tmp/recur-billing.log
./systemd-private-uEF6lO/tmp/billing.log
./systemd-private-uEF6lO/tmp/process-registration.log
$ ls systemd-private-uEF6lO/
ls: cannot open directory systemd-private-uEF6lO/: Permission denied
Observe that I have to use sudo to even see the file, because I am not running with the userid of the PHP process. This is a consequence of the decision by PHP to use the private temp directory service of systemd. This action does not appear to be described anywhere in the PHP documentation. If you call the function sys_get_temp_dir it even fibs about the location of the temp files. Note that the decision to use a private temp directory does not affect PHP scripts themselves because the temporary file names are always mapped. From the point of view of scripts the files are at the location specified by the program. That is fopen("/var/tmp/billing.log", "r"); will read the file. The problem is only with the external visibility of temporary files. I haven't checked but I suspect that on Windows PHP maps /tmp and /var/tmp to the Windows temporary directory C:\Windows\Temp\ for portability.
This is a pain because in order to make these trace files visible for external analysis I now have to define and manage my own directory to put trace files in, an action which is complicated because on my site 'DOCUMENT_ROOT' is not a valid path so I have to figure out what the real path to the document root is.
$document_root = substr(__FILE__, 0, -strlen($_SERVER['PHP_SELF']));
$trace = fopen($document_root . "/log/billing.log", "a");
Actually this is a systemd security feature: PrivateTmp
You can disable it for your specific httpd service. On Arch Linux:
juergen#samson:/tmp → grep PrivateTmp /usr/lib/systemd/system/httpd.service
PrivateTmp=true
I've recently migrated linux servers.
I'm currently getting this error when trying to upload an image using Imagick from a form.
Warning: fopen(/home/user/public/website.com/public/images/users/current/thumb/username.jpg): failed to open stream: Permission denied in /home/user/public/website.com/public/inc/functions/functions.php on line 864
The code that is run resides in a function:
try{
$im = new Imagick($image['tmp_name']);
if($im_w != 'O' && $im_h != 'O'){
$im->cropThumbnailImage($im_w,$im_h);
}
$fp = fopen($path, 'w'); //the line that is causing the error to occur. line 864.
$im->writeImageFile($fp);
return 1;
}
catch(Exception $e)
{
echo $e->getMessage();
}
I've had a look through various PHP/Linux fixes for permissions, The /images/ directory and subdirectories currently have permission: 755
Can anyone help me with a solution. Comment if you need to know anything else.
Thanks in advance.
Simply entered the command into my linux command line:
sudo chmod -R 777 /home/username/public/website.com/public/x_images/