I have tried below code, since our PHP version is little bit older 5.4.16, so this code is not working with that version, also new CurlFile($_FILES[“upfile”][“tmp_name”], $_FILES[“upfile”][“type”], $_FILES[“upfile”][“name”]), not working with php 5.4.16. any help is appreciated
$post = "@" . $_FILES["upfile"]["tmp_name"]
. ";type=" . $_FILES["upfile"]["type"]
. ";filename=" . basename($_FILES["upfile"]["name"]);
echo "<pre>";
print_r($post);
echo "</pre>";
$conn = curl_init('//third party url');
// send a file
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt(
$conn,
CURLOPT_POSTFIELDS,
array(
'file' => '@' . realpath($_FILES["upfile"]["tmp_name"]),
)
);
// output the response
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
// close the session
$result = curl_exec($conn);
$header_info = curl_getinfo($conn, CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($conn, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($conn);
2
Answers
'@' . realpath($_FILES["upfile"]["tmp_name"])
What is the @ sign for? For preventing errors? It won´t. It will append the at sign at the beginning of your filepath, which leads to the problem, that the file is not found. IF you want to prevent errors, you have to just do
@realpath(...)
, but its discouraged to use this operator.Removing it should fix your problem.
As Ingus mentioned, your Post data looks odd as well, you should not build the query string by yourself, instead use
http_build_query
, which is an inbuilt function from PHP.Use it like a so:
$post = http_build_query($_POST);
Also by “not working”, what do you mean exactly. What error are you receiving?
EDIT try with this code:
If this still does not work try to check if file is there. ..
Here is example of how i do it:
My file in this example comes from database as blob.
I hope this code helps you! Remember this is not ready to go code. Just an example. ..
Here are CURL manual: https://www.php.net/manual/en/book.curl.php