Remove file fails - Permission denied - php

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

Related

APACHE Permissions are all set but still don't have permission

I recently faced this issue, as you can see in the picture, I granted all sorts of permissions to all files, but still I get this error.
Here's the code:
$zipdir=dirname(dirname(__FILE__)).'/tmp';
$zipname="$zipdir/$input[user]-".time().'.zip';
list($pdf,$pdfname)=test_pdf($request);
$zip=new ZipArchive();
if ($zip->open($zipname,ZipArchive::CREATE)!==true) return $error='Could not open zip archive for writing';
$zip->addFromString("pdf/$pdfname", $pdf->Output('','S'));
$zip->addFile("test-docs/",$testformname);
$zip->close();
if (!file_exists($zipname)) return $error='Could not create zip archive';
https://i.stack.imgur.com/kmxUg.png
And here's the log:
PHP Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in /var/www/html/test/app/test.php
Problem just solved!
I needed to grant 775 or above permission to the whole "html" folder in this path :
"/var/www/html/..."
Not just files and folders in it because i tried that completely.
Also apache:apache permission is not needed, root does the work.
This comment helped a lot:
move_uploaded_file gives "failed to open stream: Permission denied" error

Writing Log File Error file_put_contents(/var/www/html/myproject/ulogs/my-log.log): Failed to open stream: Permission denied

I have inherited a PHP application and need to investigate various issues. I am not a PHP programmer so bear with me.
I wrote a function to log messages to a custom log file.
<?php
function my_logger($log_msg)
{
error_log("USER INFO:::::",0);
error_log(get_current_user());
error_log(exec('whoami'));
file_put_contents('/var/www/html/myproject/ulogs/my-log.log', date('G:i:s') . ">>$ " . $log_msg . "\n", FILE_APPEND);
}
After a lot of 500 errors and no information I discovered that the following line is being written to the error_log.log
PHP Warning:
file_put_contents(/var/www/html/myproject/ulogs/my-log.log): Failed to
open stream: Permission denied in
/var/www/html/myproject/app/lib/log_fns.php on line 11
I have put in a call to get_current_user and whoami which shows me:
[07-Apr-2021 14:26:04 UTC] USER INFO::::
[07-Apr-2021 14:26:04 UTC] root
[07-Apr-2021 14:26:04 UTC] apache
I am calling this at the moment from a simple php page called at http:///test_log.php:
<?php
require("/var/www/html/myproject/app/lib/log_fns.php");
//phpinfo();
my_logger("This is a test message from test_log.php");
?>
I tried to chmod the files to 777 in the ./lib directory but was still getting this error. I have hard coded the path you can see above creating the ulogs directory.
I have tried combinations of chmod the ulogs directory to 777 and chown the directory to apache:apache but still get this error.
Any ideas where I can look next or how to solve?
TIA
Run this command
sudo chmod 777 /var/www/html/myproject/ulogs/
With this work you file_put_contents can generate or edit the file in this directory
This happened to me and no changing of file/folder permissions worked. The issue in the end was SELinux, which is setup as enforcing by default on CentOS.
To disable it, edit /etc/selinux/config, and change
SELINUX=enforcing
to
SELINUX=disabled
Reboot and it should work.

'Failed to open stream: Permission denied' error - Laravel 5.4

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...

Server migration troubles with Imagick, Fopen, PHP and Permissions

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/

Permission issue with PHP on Windows share

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"

Categories