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.

<?php
// save the file with the name phpinfo.php
phpinfo();
?>

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: 192.168.106.11/app). Name it form_upload.php.

<?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(
				$upload_file['tmp_name'],
				$upload_file['type'],
				$upload_file['name']
			);
			
		} 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_RETURNTRANSFER  => 1,
		CURLINFO_HEADER_OUT     => 1,
		CURLOPT_HTTPHEADER      => $headers,
		CURLOPT_POSTFIELDS      => $postfields
	));
	
	$result = curl_exec($ch);
	
	if (curl_errno($ch)) {
		$error_msg = curl_error($ch);
		echo $error_msg;
		exit();
	}

	curl_close ($ch);
}

if($_FILES){
	
	$temp 	= explode(".", $_FILES['surat']['name']);
	$surat 	= 'FILE_'.date('YmdHis').end($temp);	
	
	// SAVE TO REMOTE SERVER
	$upload_file    = $_FILES['surat'];
	$upload_url     = "http://192.168.106.13/files/upload_to_server.php";
	$upload_filename= $surat;
	
	upload_file_remote_server($upload_file, $upload_url, $upload_filename);
	
	// SAVE TO LOCAL SERVER
	$destination 	= "./uploads/" . $surat;
	move_uploaded_file($_FILES['surat']['tmp_name'], $destination);
	
	echo 'Uploaded!';
	exit();
}

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

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

<?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)) {
			$count++; // TOTAL SUCCESS FILE UPLOADED
		}
	}
}

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.