Upload file using PHP


#1

Hello,

I’m trying to create a bulk import script using API2. The idea is that user will copy files to a temporary folder on the Razuna server and specify path to the Excel file where will be a list of all the files that need to be uploaded, and then, using upload API2 call bulk import everything to Razuna. The problem is that it looks like that upload API2 call works only with HTML web form, but I would like to upload files that are already in a temporary directory on server.

How can I accomplish this?

Here is my code:
$localFile = “D:/temp/file.jpg”;

$ch = curl_init("http://dam.eld.see:8080/razuna/raz1/dam/index.cfm");
	
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
array(
 'api_key' => "324934fake35DBD",
 'fa' => "c.apiupload",
 'destfolderid' => "D4B14C861fakeB6C96CD0D114F",
 'filedata' => '@' . $localFile
));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$meta_upd_resp = curl_exec($ch);

echo $meta_upd_resp;
	
curl_close($ch);

I get this error: Upload failed There was no appropriate FILE found in the upload.

Thank you!


#2

You have to make sure to use a FORM POST.


#3

Hello,

I have found the solution, it is possible to do it without FORM.

Thank you!

Best wishes,
Marko


#4

Great. Maybe you want to post your solution on here, so others can learn from it as well?


#5

Here is my code and it works:

        $headers = [
		"Content-Type: multipart/form-data",
		"Expect: "
	];
		
	$ch = curl_init($this->razuna_api_upload_url);
	
	$mimeType = mime_content_type($localFile);
	
	$cfile = new CURLFile($localFile, $mimeType, $filename);
	
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, [
		'api_key' => $this->razuna_api_key,
		'fa' => "c.apiupload",
		'destfolderid' => $destfolderid,
		'filedata' => $cfile
	]);
	
	// Send the request & save response to $meta_upd_resp
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$meta_upd_resp = curl_exec($ch);

	echo '<pre>test';
	echo htmlentities($meta_upd_resp);
	
	curl_close($ch);