2

I am using silm3 as my REST API framework and followed this to write my file upload script from API.

I am currently running my web app in a.xyz.com and my REST API is in b.xyz.com . Now I want to upload a picture into a.xyz.com/uploaded/ from b.xyz.com/upload.

$newfile->moveTo("/path/to/$uploadFileName");

is used to save the file in current subdomain. But I failed to upload it into another subdomain.

I search in the web, but didn't find any clue how to do it.

2 Answers2

1

After moving the file on b.xyz.com in the correct position, do the following:

// Server B

// Move file to correct position on B
$newfile->moveTo("/path/to/$uploadFileName");

// Send file to A
$request = curl_init('a.xyz.com/upload');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, [
  'file' => new CURLFile("/path/to/$uploadFileName")
]);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
curl_close($request);

a.xyz.com should then accept the file and proceed to storing it. If using Slim, in the same way as b.xyz.com does

...
$newfile->moveTo("/path/to/$uploadFileName");

or using PHP's core functions

// Server A
if ( !empty($_FILES["file"]) && !empty($_FILES["file"]["tmp_name"]) ) {
  // Move file to correct position on A
  $uploadFileName = basename($_FILES["file"]["name"]);
  move_uploaded_file($_FILES["file"]["tmp_name"], "/path/to/$uploadFileName");
}
anatoli
  • 336
  • 6
  • 14
  • Thanks for your comment. I think you missed a point. I will upload my file into ('http://b.xyz.com/upload'); first. Then I have to upload it to a.xyz.com . – Ashickur Rahman Aug 25 '16 at 03:54
  • Well, do the same thing by just inverting endpoints. Post it from B to A, and on A just move the file to the right position. – anatoli Aug 25 '16 at 10:33
  • I tried by inverting. But it is not working. Do I need to change any permission in my server? – Ashickur Rahman Aug 25 '16 at 11:14
  • Check the edited answer, try it and let me know if it works. No need to change anything in your server's config. – anatoli Aug 25 '16 at 15:45
  • Here is my code. http://pastebin.com/D01P4JZC File is uploading in the current subdomain. But it is not uploading in other subdomain. – Ashickur Rahman Aug 27 '16 at 06:21
  • Did you capture the file on the other server? – anatoli Aug 27 '16 at 08:00
  • I capture the file in b.xyz.com server. But failed to do in a.xyz.com – Ashickur Rahman Aug 28 '16 at 05:35
  • Basically what you need to do is upload the file on B, move it to the right place on B, let the server upload it from B to A, move it to the right place on A. If you still have problems, it would be better to get a chat room so we can solve them there. – anatoli Aug 28 '16 at 09:02
1

Normally domains or subdomains are restricted to their own webspace, so you can't read or write other domain's files. The setting is calles open_basedir.

You have to set it in your vhost configuration but there are also other ways, like setting it with a .htacces file if your hoster allows it: How can I relax PHP's open_basedir restriction?

Community
  • 1
  • 1
Dimitri L.
  • 4,499
  • 1
  • 15
  • 19