PHP: upload file from one server to another server

2019-01-17 13:21发布

I'm developing a website in a server and the storage is in another server and I have to deal with that somehow. I nerve experience this case and I found that the solution is to use curl.

Kindly explain to me how to use Curl in detail from zero.

Update:

I use the following code to test if cURL is installed and enabled:

<?PHP
phpinfo(); 

$toCheckURL = "http://board/accSystem/webroot/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $toCheckURL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if(!$data) {
  echo "Domain could not be found";
} else {
  switch($code) {
    case '200':
      echo "Page Found";
      break;
    case '401':
      echo "Unauthorized";
      break;
    case '403':
      echo "Forbidden";
      break;
    case '404':
      echo "Page Not Found";
      break;
    case '500':
      echo "Internal Server Error";
      break;
  }
}
?>

Here is the result: enter image description here

And I got (Page found) message

Now I can use cURL without worry, right ?

Note: Both servers are local

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-17 14:01

First, make sure the other server will accept your connections, look up Cross Domain Policy issues. Also, make sure that the file is publicly available (ie: you can download it by navigation to the URL with a standard browser).

Once everything is setup, you can use file_get_contents to get the contents of the file and file_put_contents to save it locally:

file_put_contents('file_name', file_get_contents('FILE_URL'));

Your other questions (delete, edit) are really a different beast and should most likely be handled in their own questions, as there is no way to do this from your website server alone for obvious security reasons. You would need to expose an API on the storage server and hit that API from the website to make the storage do actions the appropriate actions.

查看更多
登录 后发表回答