PHP Upload File To Remote Server Using CURL

In web-based system development, sometimes we need a feature to upload a file or document in addition to supporting the information processed on the system. This is inseparable from the file upload feature which is implemented into the system. As we know that the file storage can be placed in a special folder inside or under the root folder. But sometimes there are some cases or conditions, where we need to upload the file on another server, for example, a special server for important documents. There are also conditions when the website owner or admin wants to separate static files (images, upload files, other web assets) with the core program so that it does not affect the performance of the core program.

When we find this case, of course, we can use the FTP function in PHP to upload files to the remote server. But sometimes website owners or admins don’t want to open or use multiple ports in cases like this for server security reasons. Here I am trying to describe one of the data transmission techniques using the CURL function available in PHP.

By default, the CURL module package is available in PHP. However, there are several installation program packages that do not include CURL in their PHP packages. Here you don’t need to worry, because you can install it yourself. Previously, make sure that the CURL module package is already installed in your PHP. You can check it on phpinfo.

// save the file with the name phpinfo.php

If the curl module is not already activated, you can simply activate it by opening the php.ini configuration file and looking for this line:

; extension = php_curl.dll

Then remove the semicolon sign to be like this:

extension = php_curl.dll

Then save and restart the apache service.

After successfully installing the CURL module, we now try to create a file to send the file. This file will be placed on server A (Example: Name it form_upload.php.

// save the file with the name form_upload.php
function upload_file_remote_server($upload_file, $upload_url, $upload_filename){
	$postfields = array();
	$postfields['nama_file'] = $upload_filename;

	if (isset($upload_file['name'])) {
		if (function_exists('curl_file_create')) { // For PHP 5.5+
			$postfields["upload_file"] = curl_file_create(
		} else {
			$postfields["upload_file"] = '@' . $upload_file['tmp_name']
												  . ';filename=' . $upload_file['name']
												  . ';type='     . $upload_file['type'];
	$ch         = curl_init();
	$headers    = array("Content-Type:multipart/form-data");

	curl_setopt_array($ch, array(
		CURLOPT_POST            => 1,
		CURLOPT_URL             => $upload_url,
		CURLOPT_HTTPHEADER      => $headers,
		CURLOPT_POSTFIELDS      => $postfields
	$result = curl_exec($ch);
	if (curl_errno($ch)) {
		$error_msg = curl_error($ch);
		echo $error_msg;

	curl_close ($ch);

	$temp 	= explode(".", $_FILES['surat']['name']);
	$surat 	= 'FILE_'.date('YmdHis').end($temp);	
	$upload_file    = $_FILES['surat'];
	$upload_url     = "";
	$upload_filename= $surat;
	upload_file_remote_server($upload_file, $upload_url, $upload_filename);
	$destination 	= "./uploads/" . $surat;
	move_uploaded_file($_FILES['surat']['tmp_name'], $destination);
	echo 'Uploaded!';

echo '<form method="POST" action="?" enctype="multipart/form-data">
		<input type="file" name="surat" required>
		<input type="submit" value="Upload" name="upload">

Next, create a file to accept it’s upload parameters. This file is located on server B (Example: Name it upload_to_server.php.

// save the file with the name upload_to_server.php
$path_target 	= '';
$nama_file 		= ($_POST['nama_file'] ? $_POST['nama_file'] : date('YmdHis'));
$upload_file 	= (isset($_FILES['upload_file'])) ? $_FILES['upload_file'] : array();
$count 			= 0;

if (isset($upload_file['name'])) {
	if ($upload_file['error'] == 0) {
		$path_target= ($path_target == '' ? '' : $path_target . '/');
		$path 		= "./uploads/".$path_target;
		$name_file 	= $path . $nama_file;
		if (@move_uploaded_file($upload_file["tmp_name"], $name_file)) {

echo 'Uploaded: '.$count.' file(s)';

The code above is a modification of some of the references that I got. I got it through this link, this, and this. I am very grateful if you can provide the best input if you have a simple way in the case above. Thank you very much for visiting this article.