PHP: Move file from domain to subdomain
Asked Answered
D

4

7

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

Diastyle answered 29/6, 2010 at 11:36 Comment(1)
A personal pet peeve: I hate the term subdomain when hosting services use it as synonym for web site or, more specifically, second-class web site we allow you to have so your cheap plan looks great while we still leave market for expensive plans. :)Harry
L
4

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.

Lubberly answered 29/6, 2010 at 11:50 Comment(2)
maybe it's not hosted somewhere where it's impossible to change php.ini?Revocation
Thank you for your reply, #2 indeed looks like an option I could have a look at.Diastyle
R
0

As the error message suggests, file access is limited to certain directories with the open_basedir directive in php.ini.

If you have access to the server's configuration, you can change that.

Revocation answered 29/6, 2010 at 11:54 Comment(3)
Eh, yeah, an obvious possibility I had totally overlooked :)Harry
It's unclear to me what you want me to change in open_basedir. Could you give some extra explanations? For instance when I have /var/www/vhosts/mydomain.com/httpdocs as the main directory and /var/www/vhosts/mydomain.com/subdomains/sub/httpdocs as the subdomain?Diastyle
@Jurgen: i don't want you to change anything, since i couldn't care less. especially since you don't seem to put any effort into it yourself, not even trying to give a bit more useful information.Revocation
J
0

Why not create a symlink for your subdomain then reach images via sub.domain.com url?

Jacquelyn answered 29/6, 2010 at 12:30 Comment(0)
F
0

Most solutions here seemed a tad excessive. I've written my own solution. Take it with a grain of salt; I don't use PHP often.

// MAIN DOMAIN - index.php

/**
 * @param String $target - file name under the image you are saving
 * @param String $subdomain - target subdomain, in which, you wish to save your image
 */
function saveImage($target, $subdomain) {
    $context = stream_context_create(array('http' =>
        array(
            'method' => 'POST',
            'header' => 'Content-type: application/x-www-form-urlencoded',
            'content' => http_build_query(
                array(
                    'target' => $target,
                    'contents' => file_get_contents($_FILES['image_upload']['tmp_name'])
                )
            )
        )
    ));
    
    return file_get_contents('http://' . $subdomain . '.' . $_SERVER['HTTP_HOST'] . '/save_image.php', false, $context);
}

saveImage("test.png", "cdn");


// SUBDOMAIN - save_image.php

file_put_contents($_POST['target'], $_POST['contents']);
    
echo 'http://' . $_SERVER['HTTP_HOST'] . '/' .$target;

I now realize that this would allow for any user to make a post request to save an image to the subdomain; however, being that PHP is back-end, the easy alternative would be to pass a key declared in PHP. i.e.

// MAIN DOMAIN - index.php

$key = "A4g8S8"; // hash this and pass it through the request body


// SUBDOMAIN - save_image.php

if ($_POST['key'] == "A4g8S8") {
    // code here
}
Frediafredie answered 23/1, 2022 at 5:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.