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?

1
  • 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: stackoverflow.com/questions/3085990/… Commented May 17, 2013 at 20:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, really straight forward. Ended up just renaming it.

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.