Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Move file from domain to subdomain

I have a site on a domain, let's call it "mydomain.com", where people can log in. This domain has a subdomain sub.mydomain.com.

When the user uploads a file on the main domain, I want to move it to the uploads folder on sub.mydomain.com. The folder I want to move the file to has full write/read privileges.

My code that uploads te file:

$filename = 'background_' . $_SESSION['username'] . '.jpg';
if (is_uploaded_file($_FILES['background']['tmp_name'])) 
    {  
        move_uploaded_file($_FILES['background']['tmp_name'], 
        "/var/www/vhosts/mydomain.com/subdomains/sub/httpdocs/uploads/" . $filename);
    }

When running this code I get the following warning/error:

Warning: move_uploaded_file() [function.move-uploaded-file]: open_basedir restriction in effect. File(/var/www/vhosts/mydomain.com/subdomains/sub/httpdocs/uploads/background_User.jpg) is not within the allowed path(s): (/var/www/vhosts/mydomain.com/httpdocs:/tmp) in /var/www/vhosts/dezeactie.nl/httpdocs/opmaak.php  on line 152

Can anybody enlighten me with a solution for my problem?

One final note: I know I could do the login on the subdomain and upload the file there directly, however I would like to keep the login on the main domain for other purposes.

Thanks in advance.

Jurgen

like image 669
Jurgen Avatar asked Jun 29 '10 11:06

Jurgen


1 Answers

Your hosting service does not allow your scripts to do stuff outside the httpdocs directory of each virtual host and a shared /tmp directory. That seriously restricts the possibilities for information exchange between sites.

Tricks I can think of:

  1. Store a temporary file in the /tmp directory from the source site and trigger a process in the target site to fetch the file. You can use the curl functions to do so.

  2. Store files in a common database. The storage does not need to be permanent if you think that will affect your site performance. Target site just needs to checke whether it has pending incoming files.

  3. Add files to a queue (perhaps a special directory inside httpdocs) and write a command line script to push them to the target site. Run this command with cron (lets say, once every minutes).

IMHO, #2 looks feasible and reliable.

like image 190
Álvaro González Avatar answered Sep 30 '22 17:09

Álvaro González