Home > Backend Development > PHP Tutorial > Upload files PHP Use curl_init() to upload files on the server side

Upload files PHP Use curl_init() to upload files on the server side

巴扎黑
Release: 2023-03-03 11:56:01
Original
1443 people have browsed it

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 [&#39;DOCUMENT_ROOT&#39;].&#39;/hosts.php&#39;;//放置多台服务器的IP
$img_path = $_SERVER [&#39;DOCUMENT_ROOT&#39;].&#39;/upload/test.jpg&#39;;//图片的保存路径
$file = array("company_logo"=>&#39;@&#39;.$img_path);//文件路径,前面要加@,表明是文件上传.
foreach($hosts as $host) { 
$curl = curl_init();
//处理上传图片的URL,与客户端上传到服务器的原理是一样的
curl_setopt($curl,CURLOPT_URL,&#39;http://&#39;.$host.&#39;/upload.php&#39;);
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);
}
?>
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template