Uploading files on the client is a scenario often encountered in web development. No further details will be given here. What we want to elaborate here is how to upload files to other servers on the server side.
This scenario is often encountered when resources of multiple servers need to be synchronized, such as uploading images uploaded by users from one server
to other servers.
In fact, uploading files on the server side is not a difficult task. You can just use PHP's curl_init().
<?php require_once $_SERVER ['DOCUMENT_ROOT'].'/hosts.php';//放置多台服务器的IP $img_path = $_SERVER ['DOCUMENT_ROOT'].'/upload/test.jpg';//图片的保存路径 $file = array("company_logo"=>'@'.$img_path);//文件路径,前面要加@,表明是文件上传. foreach($hosts as $host) { $curl = curl_init(); //处理上传图片的URL,与客户端上传到服务器的原理是一样的 curl_setopt($curl,CURLOPT_URL,'http://'.$host.'/upload.php'); curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$file); curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl,CURLOPT_HEADER,0); curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE); $result = curl_exec($curl); //$result 获取页面信息 curl_close($curl); } ?>