Forum Moderators: coopster

Message Too Old, No Replies

fopen() error: failed to open stream: Permission denied

         

pizzipie

8:44 pm on Feb 17, 2020 (gmt 0)

10+ Year Member



Hi,

I am using Ubuntu 18.04.

I am trying to run a php script from an .html file in an Apache2 Virtual Host in Firefox Browser.

The script uses fopen() and I get the following error message: " failed to open stream: Permission denied".
($inputFile=fopen("optumRxInputFile.sql", "a+") or die("can't open file - line 38 test.php");)

Here are the permissions I have set:

rick@rick-Latitude-E6510:/var/www/html$ ll
total 24

drwxr-xr-x 2 root root 4096 Dec 7 10:55 ./
drwxr-xr-x 3 root root 4096 Jul 17 2019 ../
lrwxrwxrwx 1 root root 17 Dec 7 10:55 DB-sql -> /home/rick/DB-sql/

rick@rick-Latitude-E6510:~/DB-sql/PHP-Forms2$ ll
total 192

-rwxrwxrwx 1 rick rick 2364 Feb 15 11:32 optumRxForm.html~*
-rwxrwxrwx 1 rick rick 1675 Feb 17 11:00 test.php*

Here is my Virtual Host setup:

<VirtualHost *:80>

ServerName mysqlite.com
ServerAlias www.mysqlite.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/DB-sql

<Directory /var/www/html/DB-sql >

Options Indexes FollowSymLinks
Require all granted

</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Here is my test.php code:

<code> I tried to put the code in here and I keep getting 403 Error and I lose everything</code>

Don't know what else to do here to fix this.

Thanks for your help in advance.

R

lammert

8:56 pm on Feb 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You might try to put the file name including the full path in the fopen() statement. Normally the function tries to open/create a file in the current working directory of the httpd process which--at least at my Centos 8 systems--is /usr/share/httpd, not the root directory of the site.

pizzipie

2:11 am on Feb 18, 2020 (gmt 0)

10+ Year Member



I have used this but get same result.

$inputFile=fopen("/home/rick/DB-sql/PHP-Forms2/optumRxInputFile.sql", "a+") or die("can't open file - line 38 test.php");

Dimitri

11:59 am on Feb 18, 2020 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



If I don't make mistake, you are not showing the permission of the file "optumRxInputFile.sql" itself.

lammert

12:33 pm on Feb 18, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And what is also interesting to know are the settings for the /home/rick/DB-sql directory. This directory must also be readable and writeable by the Apache process.

Another possible reason for your problems is that SELinux is active. This is an extra Linux protection layer that allows or blocks actions based on application profiles. You can check this with the command sestatus

pizzipie

8:28 pm on Feb 18, 2020 (gmt 0)

10+ Year Member





* If I don't make mistake, you are not showing the permission of the file "optumRxInputFile.sql" itself.
fopen() is supposed to create it in the initiating dir PHP-Forms2.

SELinux: Command 'sestatus' not found,

** And what is also interesting to know are the settings for the /home/rick/DB-sql directory.

drwxr-sr-x 10 rick rick 4096 Feb 18 09:51 DB-sql/

lammert

8:38 pm on Feb 18, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, I didn't read your post correctly. The access rights for the PHP-Forms2 is subdirectory what I like to know :)

Dimitri

11:57 pm on Feb 18, 2020 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



If you can "create" the file, then it's because the directory does not have the writing permission for the "user" (or group) under which the scripts is being executed.

mack

11:47 am on Feb 19, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You could experiment by using "sudo chmod 777 filename.ext" that will let you know once and for all if it is a file permissions issue. But don't leave it as 777 after testing.

Mack.