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

巴扎黑
Release: 2023-03-03 11:56:01
Original
1354 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().

'@'.$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); } ?>
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!