4

Im trying to send a local file by client curl app. I found some examples to do that with files from a form. In my case, I have no form, but a local file.

$fileName = $_SERVER["DOCUMENT_ROOT"]."/www/images/test.pdf";

if(!file_exists($fileName)) {
       $out['status'] = 'error';
       $out['message'] = 'File not found.';
       exit(json_encode($out));
}
$data = array('name' => 'Foo', 'file' => '@'.$fileName);

$cURL = curl_init("http://myapi/upload-images");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($cURL);
$error = curl_error($cURL);
curl_close($cURL);

die($response);

With this, I have no erros, but in server the $_POST and $_SERVER arrays is empty.

I tried otherwise, this time creating a Curl file before send:

// Mime type of file 
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfo = finfo_file($finfo, $fileName);

$cFile = new CURLFile($fileName, $finfo, "file");

//var_dump($cFile);
//CURLFile Object
//(
//   [name] => C:/.../test.pdf
//   [mime] => application/pdf
//   [postname] => file
// )

$cURL = curl_init("http://myapi/upload-images");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS, 
array(
     'file' => $cFile
));

$response = curl_exec($cURL);
curl_close($cURL);

die($response);

Same response. $_FILES is empty.

2
  • stackoverflow.com/questions/15200632/… Commented Dec 1, 2014 at 12:34
  • Thanks for your answer @StrawHat. In my first case i tried that, by adding realpath(). Same response, $_FILES is empty in server. Commented Dec 1, 2014 at 12:40

1 Answer 1

4

Finally I found the reason of issue. The array with file data must have filedata and filename keys.

We can pass '@' before file name with full path but this is deprecated.

$data = array( "filedata" => '@'.$fileName, "filename" => basename($fileName));

In this case I added a Curl object:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfo = finfo_file($finfo, $fileName);

$cFile = new CURLFile($fileName, $finfo, basename($fileName));

$data = array( "filedata" => $cFile, "filename" => $cFile->postname);

The full code is:

$fileName = $_SERVER["DOCUMENT_ROOT"]."/www/images/test.pdf";
$fileSize = filesize($fileName);

if(!file_exists($fileName)) {
    $out['status'] = 'error';
    $out['message'] = 'File not found.';
    exit(json_encode($out));
}

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfo = finfo_file($finfo, $fileName);

$cFile = new CURLFile($fileName, $finfo, basename($fileName));
$data = array( "filedata" => $cFile, "filename" => $cFile->postname);

$cURL = curl_init("http://myapi/upload-images")
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

// This is not mandatory, but is a good practice.
curl_setopt($cURL, CURLOPT_HTTPHEADER,
    array(
        'Content-Type: multipart/form-data'
    )
);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $data);
curl_setopt($cURL, CURLOPT_INFILESIZE, $fileSize);

$response = curl_exec($cURL);
curl_close($cURL);


die($response);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.