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); ?>
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!