2

I currently have a site which allows file uploads. When the user submits the form, I run a php script that sends off a curl request to my api.

Currently the php request looks like this:

$params = array(
    'media' => "@" . $_FILES['media']['tmp_name']
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

And the api just checks the $_FILES field and then grabs media and runs move_uploaded_file.

So the file goes straight from user submitted form to curl to api server, with the file never being actually uploaded (besides being placed in a tmp folder between form submit and curl request) until it hits the api server.

This all works fine for uploading the file to the api server, but the problem is that the server thinks the file's extension is .tmp, as opposed to something like a png, because that's the file the curl request is sending.

How can I send the file without first uploading it pre-curl so that the api server knows what file is actually being sent?

Lucas Raines
  • 1,315
  • 3
  • 15
  • 24
  • Just to make it clear, `being placed in a tmp folder` actually means `being uploaded to your server`. You can work with it and as an option you can read it and post it as a string, see here: http://stackoverflow.com/questions/3085990/post-a-file-string-using-curl-in-php – Ranty May 17 '13 at 20:27

1 Answers1

2

Two ways:

  • Rename the tmp file. It is accessible to you, so renaming it is trivial: rename(oldname, newname)
  • Send a mimetype. The format is: @filename;type=image/png for a png.

Prefer the first option if you care about the filename, option two if you only care about the mime type.

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66