PHP isn't able to read file - php

I'm stuck trying to open a file with fopen in php.
$db_ausgaenge = "statuseing.php";
$dout = fopen($db_ausgaenge, "x+");
print $dout;
print(shell_exec('whoami'));
print(shell_exec('pwd'));
print(shell_exec('id'));
fwrite($dout, $out);
fclose($dout);
Warning: fopen(statuseing.php): failed to open stream: File exists in /var/www/html/zufallsgenerator.php on line 33
I checked following items:
chmod for statuseing.php 0777
owner is www-data with groud www-data
script is running as user www-data
groups are uid=33(www-data) gid=33(www-data) groups=33(www-data)
pwd is /var/www/html as expected
the path the scripts want's to open is correct
checked openbase dir in php.ini showed in phpinfo(), added /var/www/html, but php doesn't care about it.
open_basedir = /var/www/html/
After daemon-reload and restarting apache2 via systemctl nothing changed, phpinfo() didn't show the path given in the config. restarting the system via init 6 didn't took effect, too.

statuseing.php already exists.
See the manual (http://php.net/manual/en/function.fopen.php) - opening in x or x+ mode says: Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE

Look at the mode you are using.
x+ means that if the file already exists an error will be thrown.
To find the correct mode depending on your scenario check out http://php.net/manual/en/function.fopen.php

Try this:
$db_ausgaenge = __DIR__."/statuseing.php";
$dout = fopen($db_ausgaenge, "a+"); // x+ will throw error cuz it tries to open existing file, thx to: bluegman991 (;
print(shell_exec('whoami'));
print(shell_exec('pwd'));
print(shell_exec('id'));
fwrite($dout, $out);
fclose($dout);
or if You want to truncate file before adding data use w+:
$db_ausgaenge = __DIR__."/statuseing.php";
$dout = fopen($db_ausgaenge, "w+");
print(shell_exec('whoami'));
print(shell_exec('pwd'));
print(shell_exec('id'));
fwrite($dout, $out);
fclose($dout);
also do some checkups:
1) check free space: df -h
2) check if You can edit that file: nano /var/www/html/statuseing.php

Related

tmp files in laravel 8 public folder

When I upload files (move actually) to the public folder in laravel 8, they are stored as .tmp files and when I attempt to store files in storage folder, it says failed to open stream: permission denied. Is there some kind of write permission I should enable? or am I missing something in the code?
if($request->file('file')) {
$uniqueFileName = uniqid() . $request->file('file')->getClientOriginalName();
$post = new Post();
$post->name = request('name');
$post->email = request('email');
$post->doc = $uniqueFileName;
$request->file('file')->move(public_path('uploads/files',$uniqueFileName));
//I also tried to store files here, but says "failed to open stream: permission denied"
//$request->file('file')->store('uploads');
$post->save();
return redirect()->back();
}
You need to change the permissions using this command: chmod -R 777 storage
Try on terminal sudo chmod -R 777 public it will change the directory permission
had the same issue with a similar approach.
fixed it by manually changing the php.ini
changed the entry upload_max_filesize = 2000M
! dont try to change it within your scripts with ini_set(), it doesnt work for this entry. why? -> check changeable https://www.php.net/manual/en/ini.list.php !
mby php checks the file_size AFTER its uploaded and denies permission if it exceeds the value which is set? dont know. but works for me...
some folks may also run into:
PHP Warning: POST Content-Length of XXX bytes exceeds the limit of XXX bytes in Unknown on line 0
fixed it by manually changing the php.ini
changed the entry post_max_size = 2000M

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.

session_start() returns "No such file or directory" directly after creating the session file

I'm trying to set the session.save_path on my IIS/PHP via fastcgi server. So I created a new folder called tmp in my php folder, gave IUSR and IIS_IUSRS full permissions to this folder, and set my session.save_path variable in php.ini to "\tmp".
Upon loading my webpage I get the error:
Warning: session_start(): open(tmp\sess_gp13t5fg969iddfq1lrt3e88o1, O_RDWR) failed: No such file or directory (2)
The weird thing is I know it can see the folder because I look in my newly created tmp folder and it actually created the sess_gp13t5fg969iddfq1lrt3e88o1 file right before throwing the error telling me it doesn't exist. So how can it create the file and then not see it? What could I be doing wrong?
Update: If I set session.save_path with an absolute path, it works. When I give it a relative path of just "\tmp" it no longer works. Every example I see uses a relative path for the save_path and I personally need to use a relative path. Why would a relative path cause this issue and how do I fix it?
I found the answer you seek:
Relative Paths in PHP
include($_SERVER["DOCUMENT_ROOT"] . "/relative path here");
Under linux the PrivateTmp config of systemd may be the problem.
The solution would be to copy the corresponding systemd unit file to /etc/systemd and remove there the privatetmp config.
In my case:
cp /usr/lib/systemd/system/httpd.service /etc/systemd/system
systemctl daemon-reload
systemctl restart httpd.service
Change in /etc/systemd/httpd.service then
PrivateTmp=true
to
PrivateTmp=false

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.

Reading file from php module fails with errno 13

Good day.
There is a PHP module ( .so) loaded within PHP. On MINIT stage it tries to read a file.
The file is a /tmp/aaa.txt
The directory /tmp belongs to root and its permissions are set to 777.
The file /tmp/aaa.txt belongs to apache user and is also set to 777 permissions.
Module opens the file with VCWD_FOPEN(), which is define for
#define VCWD_FOPEN(path, mode) virtual_fopen(path, mode TSRMLS_CC) which eventually is a fopen().
The VCWD_FOPEN fails with error 13 (permission denied).
The strange thing is, if I invoke the module manually
( #php -r 'echo "hi";' ) - it works.
But when it runs from apache - it doesnt.
Anybody knows why?
Thank you
Found the problem.
The user permission policy was enforced by SELinux.
To disable it i typed
#setenforce 0
#service httpd restart
Works now

Categories