3

I finished writing a website and am currently deploying it on a hosting service. This website consists out of 2 separate domains hosted on the same server. (www.domain1.com & www.domain2.com)

Situation

When moving these domains to the hosting service, I figured that 1 script is not working as it was on the local server. This script is a custom upload class in php. This script is located on www.domain1.com but needs to upload the file to www.domain2.com.

My local server is a Xampp server with a simple file structure.

C:/xampp/htdocs

  • Domain1/uploadscript.php

  • Domain2/destinationfolder/

I am currently using the $_SERVER['DOCUMENT_ROOT'] function to navigate towards the other domain. This method works fine for the local server but not on the hosted version see below.


Main issue

Local server print_r ($_SERVER['DOCUMENT_ROOT']);

= C:/xampp/htdocs

On domain1.com print_r ($_SERVER['DOCUMENT_ROOT']);

= /home/myname/domains/domain1/public_html/

On domain2.com print_r ($_SERVER['DOCUMENT_ROOT']);

= /home/myname/domains/domain2/public_html/

The $_SERVER['DOCUMENT_ROOT'] is not pointing to the "domains" folder but already is pointing to the "public_html".


The question(s)

How do I navigate towards the "domains" folder on my server? Are there other things necessary for cross domain uploading?

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
NealVDV
  • 2,302
  • 3
  • 26
  • 51
  • I'm not sure that understand correctly what "**How do I navigate towards the "domains" folder on my server**" means. Can you explain please ? – Sergio Ivanuzzo Nov 22 '15 at 22:39
  • @SergioIvanuzzo Sure :)! In short how can I code my way from the domain1/public_html/ to domain2/public_html. Currently I am trying this with the `$_SERVER['DOCUMENT_ROOT']` but using this in a script on domain1 takes me to the /home/myname/domains/domain1/public_html/. So for me it is impossible still to navigate from within the script on domain1 to the destination on domain2. I need this because I use the `move_uploaded_file($tempname, $destination)` method to move the uploaded file to the destination. Thanks for the help! – NealVDV Nov 23 '15 at 23:57
  • It seems you are doing something strange. For sending data from domain A to domain B all you need is **simple form** on domain A and **request handler** on domain B. And also you need to configure CORS (as I write in answer below) – Sergio Ivanuzzo Nov 24 '15 at 10:21
  • @SergioIvanuzzo Didn't know that it was done like this :) I have tried to find a good example of such a **request handler** but I'm not sure if I am searching the correct thing... Could you point me in the right direction? Thank you! – NealVDV Nov 24 '15 at 11:56
  • ok, I'll try. You can put your **uploadscript** to domain B. On domain A you need only form which allow you to upload file (don't forget to define enctype="multipart/form-data" in this form). You also need to set action as "http:// domainB/uploadscript". That's all. – Sergio Ivanuzzo Nov 24 '15 at 12:05

1 Answers1

1

If you want to send some cross domain data, you should set Access-Control-Allow-Origin header.

You can do this in your .htaccess like below:

Header set Access-Control-Allow-Origin "*"

Be sure that you enabled mod_headers in your apache server.

For customizing access you can use something like https://stackoverflow.com/a/10605009/5397119.

See also https://stackoverflow.com/a/22331450/5397119

Community
  • 1
  • 1
Sergio Ivanuzzo
  • 1,820
  • 4
  • 29
  • 59
  • Hi again, I tried to reallocate the upload script as you mentioned. So the current situation is form is on **domain1** and the upload script is located on **domain2**. The `enctype` is in place (was already set). The Header is set like the line supplied above. Now when I try the code an internal server error _500_ even before the script runs. I figure that it might be something with this Header set Access-Control-Allow-Origin "*". How do allow the "mod_headers"? Thanks! – NealVDV Nov 25 '15 at 11:32
  • Hi again, I tried to find an error log but didn't find anything useful. I have solved the issue in another way. I want to thank you for your effort! – NealVDV Nov 27 '15 at 13:10
  • @NealVDV hi. very good. btw, you can post your solution as answer and accept it. – Sergio Ivanuzzo Nov 27 '15 at 14:20