1
Hello, I need to send via Curl multiple images, which will be received in an API, but in the API it receives in a specific way, in which I am not able to send by Curl. Follows form the API receives the image:
foreach ($_FILES as $file) {
$arquivo_enviar = $file['name'];
$extensao_arquivo = strrchr($arquivo_enviar, '.');
$arquivo_enviar = md5(time().$arquivo_enviar) . $extensao_arquivo;
if (in_array($file['type'], $allowedExts)) {
if ($file["error"] > 0) {
http_response_code(406);
} else{
$de = $_SERVER['DOCUMENT_ROOT'].'everton/api/uploads/produtos_'.md5($data->authenticator).$id_loja.'/'. $arquivo_enviar;
move_uploaded_file($_FILES["file"]["tmp_name"], $de);
$caminho[$i] = 'http://192.168.110.4/everton/api/uploads/produtos_'.md5($data->authenticator).$id_loja.'/'.$arquivo_enviar;
in this case, the $_FILES contains the following information:
Array(
[file] => Array
(
[name] => blog_pense_digital_gif.gif
[type] => image/gif
[tmp_name] => /tmp/phpkoZilo
[error] => 0
[size] => 761850
))
At the moment, I’m sending, by Curl, as follows:
$url = $url_api.'produto/'.$produtoId.'/uploadimage?authenticator='.$token;
$imagens = count($_FILES["file"]["name"]);
for ($i = 0; $i < $imagens; $i++){
$filename = $_FILES['file']['name'][$i];
$filedata = $_FILES['file']['tmp_name'][$i];
$filesize = $_FILES['file']['size'][$i];
if ($filedata != ''){
$headers = array("Content-Type:multipart/form-data");
$postfields = array("filedata" => "@$filedata", "filename" => $filename);
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $options);
print curl_exec($ch);
curl_close($ch);
}
}
Only the API is getting my file as follows:
Array(
[filename] => Sem título.jpg)
Array(
[filedata] => Array
(
[name] => phpKERYcS
[type] => application/octet-stream
[tmp_name] => /tmp/phpggdoxS
[error] => 0
[size] => 168099
))
The filename is being sent separately from the rest, via POST, and filedata, with the file information via FILE
Please enter your code in a codePen so that we can view it better and so we can help you?
– Maurício Krüger