Ashickur Rahman
Ashickur Rahman

Reputation: 127

How to upload a file into another server in slim3 php

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.

Upvotes: 2

Views: 669

Answers (2)

anatoli
anatoli

Reputation: 337

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");
}

Upvotes: 1

Dimitri Lavrenük
Dimitri Lavrenük

Reputation: 4889

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?

Upvotes: 1

Related Questions