yarek
yarek

Reputation: 12064

php copy function does not work when called from http

my php function works when called inside the shell

php script.php

Here is what is in script.php:

copy("/home/mysite/public_html/streams/videos/1_video.flv","/home/mysite2/public_html/uploads/recordings/video.flv");

But when running my script through http, then copy does not work.

I guess some permissions problems that I cannot solve.

Any idea?

I got the error/warning:

failed to open stream: Permission denied.

Upvotes: 0

Views: 234

Answers (1)

Giacomo1968
Giacomo1968

Reputation: 25994

But when running my script through http, then copy does not work.

Yes, when you run the script via the command line it will run under your individual user permissions.

But when you run the same script via http—meaning it goes through the Apache server—then the user connected to Apache needs to have read permissions from the source & read/write permissions for the destination.

So chances are either other the source or destination is not properly set for the Apache web server user. So assuming your Apache user is www-data then change the ownership of the files in /home/mysite/public_html/ like follows:

sudo chown www-data -R /home/mysite/public_html/

There are two basic ways you can determine who the Apache user is on the server. One is via the command line like this:

ps aux | egrep '(apache|httpd)'

Which will return a list of processes that look like this:

www-data 13047  0.0  1.9 405496 11628 ?        S    Jun15   0:00 /usr/sbin/apache2 -k start
www-data 13048  0.0  1.8 405280 11248 ?        S    Jun15   0:00 /usr/sbin/apache2 -k start
www-data 14547  0.0  1.8 405488 11028 ?        S    Jun15   0:00 /usr/sbin/apache2 -k start
www-data 14649  0.0  1.8 405520 11044 ?        S    Jun15   0:01 /usr/sbin/apache2 -k start
www-data 16155  0.0  1.8 405496 11200 ?        S    Jun17   0:00 /usr/sbin/apache2 -k start
www-data 29323  0.0  1.7 405240 10876 ?        S    Jun18   0:00 /usr/sbin/apache2 -k start

The user will be the first entry on each line. So in this case www-data is the user.

Another way to handle determining the user in PHP is too create a small PHP file named something like whoami.php like this:

sudo nano whoami.php

And then place this simply script in that file:

<?php
echo exec('whoami');
?>

When you load whoami.php in a web browser you will see the output of whoami which in my case is www-data.

Upvotes: 1

Related Questions