Server migration troubles with Imagick, Fopen, PHP and Permissions - php

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/

Related

error in reading file PHP and Apache server and Linux

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.

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

throw new FileException('File must be readable.');

I'm using Laravel , The following code works on my local machine (Mac)
$Avatarpath = base_path()."/uploads/image/avatar.png"
$filePath = base_path()."/uploads/image/myimage.png" //which gives "/var/www/myproject/uploads/image/myimage.png"
return Response::download(file_exists($filepath) ? $filepath :$Avatarpath);
but when I deploy the same code on linux (Centos) server , it throws the following Exception
throw new FileException('File must be readable.');
Additional info: "uploads" folder has drwxr-xr-x (775) permissions
Thank you your interest to fix this issue.
Important is that the permissions for the file itself are correct too!
Usually in this cases you should run chmod with the recursive flag (-R):
chmod -R 775 uploads/
This Exception is thrown if function http://php.net/manual/en/splfileinfo.isreadable.php is returning false.
Your file permissions must be denying the file read.

Mac OS X permissions to create a zip file

After searching the internet find an answer I've decided to ask for help directly.
I am running mac os x 10.7, PHP, Apache and MySQL. I am trying to let a user select files from a form have them zipped and sent to the individual. At first PHP couldn't even create a file. I found the following:
sudo chmod -R +a "_www allow read,write,append,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit" ~/Sites
Which allowed me to create a file. But when I try to create a zip I get some unknown extension being appended at the end and 2 files are being created. So not knowing what was wrong I thought I would just find the file by opening the directory and forcibly renaming it back to *.zip. I have checked the file after renaming manually and it seems to be fine.
I get the following error:
Warning: rename(tmp/RosenData082112-1102.zip.ohoKRw,RosenData082112-1102.zip) [function.rename]: Permission denied in /Users/mmcri/Sites/rosenlab/download.php on line 42
ls -le reveals this to the files:
-rw-------+ 1 _www staff 43775 Aug 21 11:02 RosenData082112-1102.zip.1qJUE4
0: user:_www inherited allow read,write,append,readattr,writeattr,readextattr,writeextattr,readsecurity
-rw-------+ 1 _www staff 43775 Aug 21 11:02 RosenData082112-1102.zip.ohoKRw
0: user:_www inherited allow read,write,append,readattr,writeattr,readextattr,writeextattr,readsecurity
Here is the PHP code section making the zip file:
if(extension_loaded('zip'))
{
$zip = new ZipArchive();
$zipname = "RosenData".date("mdy-Hi").".zip";
if($zip->open($direct.$zipname, ZIPARCHIVE::CREATE) === TRUE)
{
foreach($filelist as $file)
$zip->addFile($path.$file,$file) or die ("Error adding file: $file");
$zip->close();
}
// Random stuff below
}
Any help is greatly appreciated.
Thanks.
Do you have permissions to write on that folder in Mac?
Try with fopen instead of ZipArchive.

Categories