Home > Backend Development > PHP Tutorial > How Can I Transfer Uploaded Files to Another Server Using cURL in PHP?

How Can I Transfer Uploaded Files to Another Server Using cURL in PHP?

Susan Sarandon
Release: 2024-12-14 01:59:08
Original
886 people have browsed it

How Can I Transfer Uploaded Files to Another Server Using cURL in PHP?

Transferring Uploaded Files Using cURL in PHP

You have a web form with a file upload button and an accompanying PHP script that processes the uploaded file. However, you now need to forward the file to another server. This article provides a solution to accomplish this task.

To upload the file, you can utilize cURL with the following steps:

<?php
if (function_exists('curl_file_create')) { // php 5.5+
  $cFile = curl_file_create($file_name_with_full_path);
} else { // 
  $cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents' => $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
?>
Copy after login

For additional information, you may also refer to:

https://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

Note for PHP 5.5 :

For this version of PHP, you should use https://wiki.php.net/rfc/curl-file-upload instead. However, if you still want to follow the deprecated approach described above, ensure to set curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); in your script.

The above is the detailed content of How Can I Transfer Uploaded Files to Another Server Using cURL in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template