Curl PHP Send Image

Php upload image to remote server with cURL

Here's a possible solution;

  • Handle the upload on your web server and move the uploaded file to a local temp location
  • Then make a curl POST request to remote server and tell it what the uploaded file name & data is; as base64_encoded string
  • Remote server script receives the upload as a standard http post
  • All it now has to do is decode the file data, save it as the specified file name

So, this is how the solution looks like:

Sorry, i did not test this, but it should work.

index.php

<?php

// Handle upload
if(isset($_POST["submit"]))
{
// Move uploaded file to a temp location
$uploadDir = '/var/www/uploads/';
$uploadFile = $uploadDir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile))
{
// Prepare remote upload data
$uploadRequest = array(
'fileName' => basename($uploadFile),
'fileData' => base64_encode(file_get_contents($uploadFile))
);

// Execute remote upload
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://1.1.1.1/receiver.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $uploadRequest);
$response = curl_exec($curl);
curl_close($curl);
echo $response;

// Now delete local temp file
unlink($uploadFile);
}
else
{
echo "Possible file upload attack!\n";
}
}

?>

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="index.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

Then, on the receiver.php, you can do the following:

// Handle remote upload
if (isset($_POST['fileName']) && $_POST['fileData'])
{
// Save uploaded file
$uploadDir = '/path/to/save/';
file_put_contents(
$uploadDir. $_POST['fileName'],
base64_decode($_POST['fileData'])
);

// Done
echo "Success";
}

Using curl and php script to upload an image and text

Okay, so I found the issue. You'll need to use the CurlFile() class, instead of the '@' . $file_name example.

Docs: http://php.net/manual/en/class.curlfile.php

Instead of

$postfields['file_contents'] = '@' . $filename;

You'll want to do this

$postfields['file_contents'] = new CurlFile($filename);

You can look at the docs for specifying mime type and name on the other end in the constructor, but that's the basic gist of it.

Best of luck!

Image upload CURL command to PHP Curl

I fixed the issue myself. Had to use php curl file create to make it work instead of appending with '@'

http://www.php.net/manual/es/function.curl-file-create.php

Send file via cURL from form POST in PHP

Here is some production code that sends the file to an ftp (may be a good solution for you):

// This is the entire file that was uploaded to a temp location.
$localFile = $_FILES[$fileKey]['tmp_name'];

$fp = fopen($localFile, 'r');

// Connecting to website.
$ch = curl_init();

curl_setopt($ch, CURLOPT_USERPWD, "email@email.org:password");
curl_setopt($ch, CURLOPT_URL, 'ftp://@ftp.website.net/audio/' . $strFileName);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'CURL_callback');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
curl_exec ($ch);

if (curl_errno($ch)) {

$msg = curl_error($ch);
}
else {

$msg = 'File uploaded successfully.';
}

curl_close ($ch);

$return = array('msg' => $msg);

echo json_encode($return);

How to use PHP cURL to send images with the correct Content-Type?

Use the CURLOPT_HTTPHEADER option:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));

So specify Content-Type headers specifically for file uploads, use:

$params = array('name'=>'@D:\globe.jpg;type=image/jpeg');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

Upload image to PHP server using cURL?

Change the php code to

<?php
$file = date("YmdHisms") . ".jpg";
move_uploaded_file($_FILES['image']['tmp_name'], $file);
?>

Use CURL to send a file and parameters in PHP

Uploading files with cURL will become available like just a regular HTTP FILE POST and should be available through $_FILE global variable to be handled the same regular way you'd handle a regular file upload with PHP.

test.php

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, "http://localhost/Projects/Test/test-response.php");
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

curl_setopt($cURL, CURLOPT_POSTFIELDS, [
"ID" => "007",
"Name" => "James Bond",
"Picture" => curl_file_create(__DIR__ . "/test.png"),
"Thumbnail" => curl_file_create(__DIR__ . "/thumbnail.png"),
]);

$Response = curl_exec($cURL);
$HTTPStatus = curl_getinfo($cURL, CURLINFO_HTTP_CODE);

curl_close ($cURL);

print "HTTP status: {$HTTPStatus}\n\n{$Response}";

test-response.php

print "\n\nPOST";
foreach($_POST as $Key => $Value)print "\n\t{$Key} = '{$Value}';";

print "\n\nFILES";
foreach($_FILES as $Key => $Value)print "\n\t{$Key} = '{$Value["name"]}'; Type = '{$Value["type"]}'; Temporary name = '{$Value["tmp_name"]}'";

Output

HTTP status: 200

POST
ID = '007';
Name = 'James Bond';

FILES
Picture = 'test.png'; Type = 'application/octet-stream'; Temporary name = 'C:\Windows\Temp\php76B5.tmp'
Thumbnail = 'thumbnail.png'; Type = 'application/octet-stream'; Temporary name = 'C:\Windows\Temp\php76C6.tmp'

I assume you will keep the relevant 2 images in the same path as 'test.php' to obtain the output as shown.



Related Topics



Leave a reply



Submit