PHP Script

On a CENT OS box 7:

I have a script that when I run from the SSH works perfects...but running from a browser or cron does not work. I get the following error:

When i run my php page in Browser getting the following error ::

Warning: fopen(property.csv): failed to open stream: Permission denied in /var/www/html/*****/download.php on line 44

Unable to create property.csv ..

I run the same page in SSH :: Using Following command

php /var/www/html/*****/download.php

It run perfectly .. and created csv file.. But not working when i run this page in browser or cron
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Radek BaranowskiFull-stack Java DeveloperCommented:
I guess the reason must be different users running  this php code in both cases.

you must be running your apache/php server as a user who has no permission to write file to /var/www/html/***** directory

check what user runs apache/php (ps -ef | grep httpd) then compare to the user you run shell command as.

compare them users to see what permissions you are lacking. also, there might be some default php settings enforcing some particular limitations in writing foiles to disk.
Sir LearnalotCommented:
Did you check the permissions of the file? Run ls -l in the directory of the files to see the permissions. Set them correctly if needed with chmod xxx filename where xxx is a numerical value for Read, Write, and Execute.
Vimal DMSenior Software EngineerCommented:

There are two things you need to look at,

1) Running from browser

htaccess might have been added to this website, which might be blocking the "download.php" direct access from the browse.

2) Running from CRON

Permission needs to be given for the folder or file.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Starquest321Author Commented:
(1) What command steps in detail can I take to check for option 1.
(2) I have given the folder and file ALL permissions. So how is that possible?
Sir LearnalotCommented:
if all permissions are desired enter chmod filename/folder -R 777

where chmod is command to set permissions
filename/folder is desired object to set permissions on
-R sets permissions recursively (on all child objects / sub folders and files)
777 = permissions full read, write, and execute for current user, all users, and admin users
Starquest321Author Commented:
Radek Baranowski: I ran the user and this is the result I get:
ps -ef | grep httpd
root      1073     1  0 Sep13 ?        00:00:12 /usr/sbin/httpd -DFOREGROUND
apache   11236  1073  0 03:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11256  1073  0 03:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11257  1073  0 03:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11258  1073  0 03:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11259  1073  0 03:22 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11272  1073  0 03:27 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11274  1073  0 03:27 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11275  1073  0 03:27 ?        00:00:01 /usr/sbin/httpd -DFOREGROUND
apache   11276  1073  0 03:27 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11464  1073  0 03:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11465  1073  0 03:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root     12813 12782  0 12:11 pts/0    00:00:00 grep --color=auto httpd

In the folder. . . this is the permissions:
drwxrwxrwx. 2 root root   4096 Sep 15 03:58 rets

In the file:
-rwxrwxrwx. 1 root root  3412 Sep 15 04:33 download.php
Starquest321Author Commented:
The simply issue: When I run the page in putty its runnign perfect. But in browser giving this error:

Warning: fopen(property_res.csv): failed to open stream: Permission denied in /var/www/html/application/download.php on line 44

I have given all permissions to folder rets .. still no luck...
Starquest321Author Commented:
09 * * * * root /var/www/html/applicationrets/download.php

Tried a full path as well.
Radek BaranowskiFull-stack Java DeveloperCommented:
are you using 'w' flag for writing in fopen ?

$output = fopen('php://output', 'w');
Starquest321Author Commented:
Yes . .using W+.
It will be an empty file and we will start writing to that file.  If the file does not exist it will create.
Dave BaldwinFixer of ProblemsCommented:
It is simply that when you are using SSH, you are logged in under your own user and have permissions to do what you want in your own directories.  When you are running thru Apache, the Apache user is Intentionally limited to what it can do.  This is to prevent malicious people from hacking into your site.
Starquest321Author Commented:
even if I give user:: group as root I still have the same issue. Coult it be that first issue with htaccess?
I expect Dave is write, but maybe we are looking at the wrong thing.  It might be the references in the script are what is causing the problem.  You have not posted the script and if the code is the source of the program, then all the time being spent guessing and speculating is wasted if there is something simple that can be solved by looking at the code.

Starquest321Author Commented:
The only logical question I have: Why would it work from SSH? That does not make sense. If the script is broken . . how will it allow itself to run from anywhere. . .
Dave BaldwinFixer of ProblemsCommented:
There is no reason to think that the script is broken.  You're not understanding Linux permissions.  SSH requires a user login and anything you run while connected thru SSH runs with the permissions of that user.  Apache runs as it's own very limited user and does not have permissions outside of the web directories.  Setting the 'group' to root has no effect on those permissions.  The 'normal' solution is to use 'su_exec' which allows PHP to run under the permissions of the current user but that is normally used when you are running sites in '/home/user/public_html/'.  I don't know if it works in /var/www/html/ .
Sir LearnalotCommented:
I believe Dave is correct.
Starquest321Author Commented:
I also don't believe the script is broken. It works fine. Right now I can make the owner :: group - root or apache....just stuck here. What to do?

Script works perfect when logged in as root and using SSH
Radek BaranowskiFull-stack Java DeveloperCommented:
post it here, then we could say something more.
Starquest321Author Commented:
The trouble area:

            echo "+ {$resource}:{$class}<br>\n";

        $file_name = strtolower("{$resource}_{$class}.csv");
        $fh = fopen($file_name, "w+");

        $maxrows = true;
        $offset = 1;
        $limit = 50;
        $fields_order = array();
Starquest321Author Commented:
Warning: fopen(property.csv): failed to open stream: Permission denied in /var/www/html/application/rets/download.php on line 44
Radek BaranowskiFull-stack Java DeveloperCommented:
well not much more known..

one thing: we have all focused on the directory /var/www/html/application/rets/, although it's only the place of residence of download.php script.

but where does it try to create the file ? where the file is created when you run it from shell ? I suppose that when you run it with shell/php then file gets created in the directory you run the command from

this probably - most certainly - is not the case when you run it via browser. would it be php temp dir ? or any of these two:

; The root of the PHP pages, used only if nonempty.
; if PHP was not compiled with FORCE_REDIRECT, you SHOULD set doc_root
; if you are running php as a CGI under any web server (other than IIS)
; see documentation for security issues.  The alternate is to use the
; cgi.force_redirect configuration below
doc_root =

; The directory under which PHP opens the script using /~username used only
; if nonempty.
user_dir =
Starquest321Author Commented:
Are these settings under php.ini?
Radek BaranowskiFull-stack Java DeveloperCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.