Jump to content

Curl Upload


steviez

Recommended Posts

Hi,

 

I am integrating some api code for my site and am getting stuck on this curl upload script. The actual upload function works perfect on ,y site so i have come to the conclusion that its the following code:

 

<?php

// Example Upload API (PHP with cURL)

class Uploader 
{
    var $file;
var $uploadURL;
var $api_key;
var $postParams = array ();

/* Constructor for Uploader */
function Uploader($file, $uploadURL, $api_key) 
{
	$this->uploadURL = $uploadURL;
	$this->postParams = array("api_key" => $api_key, "file" => "@" . $file);
}

/* function to upload file */
function UploadFile() 
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $this->uploadURL);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postParams);
	$postResult = curl_exec($ch);
	if(curl_errno($ch)) 
	{
		print curl_error($ch);
		print "<p>Unable to upload file.</p>";
		exit();
	}
	curl_close($ch);
	return $postResult;
}
}

// set upload server
$upload_server = "http://mysite.com/api/upload.php";
$upload = new Uploader('file.rar', $upload_server, '546474a6ge5dfe958575857');
$result = $upload->UploadFile();

// error print
if(preg_match("/upload_failed/", $result)){ echo "<p>Upload failed.</p>"; }
if(preg_match("/invalid_api/", $result)){ echo "<p>Invalid API Code.</p>"; }

// success print
if(preg_match("/upload_success/", $result)){ echo "<p>" .$result. "</p>"; }

?>

 

 

does this look ok to you to perform a file upload? all i keep getting is upload_failed

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/197454-curl-upload/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.