Page 1 of 1

using curl to upload a file to a server

Posted: Thu Sep 27, 2007 11:11 am
by jasongr
Hi People,

I tried to write a function that will allow me to use the curl library to upload files to a server.
Here is the function

Code: Select all

function fileupload($url, $cookie, $postData, &$result, $referer, $timeoutInSeconcs = 20)
{
	// Initialize the curl library
	$ch = curl_init();
	if ($ch === false) {
		return ERROR_BAD_CURL_CONNECTION_HANDLE;
	}
	
	// Calculate the size of the payload in bytes
	// The path to the payload file is one of the POST parameters in the array
	$contentSizeInBytes = 0;
	$filePath = '';
	foreach ($postData as $data) {
		if (is_file($data) && file_exists($data)) {
			// This is the payload file.
			$filePath = $data;
		}
		else {
			$contentSizeInBytes += strlen($data);
		}
	}

	if ($filePath == '') {
		// Failed to find the payload in the POST parameters array
		return ERROR_BAD_PARAMETER_VALUE;
	}

	// Add the size of the payload to the total content length
	$fileSizeInBytes = filesize($filePath);
	$contentSizeInBytes += $fileSizeInBytes;

	if ($contentSizeInBytes == 0) {
		return ERROR_BAD_PARAMETER_VALUE;
	}

	// Obtain a handle to the payload file. This is needed for the CURLOPT_INFILE option
	// Question: Should I be using the 'r' attribute?? Maybe a different attribute should be used instead...
	$fileHandle = fopen($filePath, "r");
	if ($fileHandle === false) {
		return ERROR_BAD_PARAMETER_VALUE;
	}

	// Set the curl parameters
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeoutInSeconcs);
	curl_setopt($ch, CURLOPT_REFERER, $referer);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_UPLOAD, true);
	curl_setopt($ch, CURLOPT_INFILESIZE, $fileSizeInBytes);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
	curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-Type: multipart/form-data; boundary=---------------------------7d7b7105ea0454; Content-Length:' . $contentSizeInBytes));
	curl_setopt($ch, CURLOPT_INFILE, $fileHandle);

	// Invoke curl	
	$result = curl_exec($ch);

	// Close the handle to the payload file
	fclose($fileHandle);

	$curlErrorCode = curl_errno($ch);
	$curlErrorMessage = curl_error($ch);

	if($curlErrorCode == CURLE_OPERATION_TIMEOUTED) {
		return ERROR_COMMUNICATION_TIMEOUT;
	}

	return SUCCESS;
}
In case of success, the function will return SUCCESS and variable $result will contain the HTTP response that curl got.
For some reason the function is not working for me and I keep getting the following value in the $result variable:

Code: Select all

HTTP/1.1 100 Continue
even though I get SUCCESS from the function.

Can anyone see what could be the problem with the function?

The $postData is an array of all the parameters that I need tos submit in the form (request method is POST)

Thanks in advance
Jason