How to upload a file with PHP?

Asked

Viewed 246 times

0

I have a function to perform file upload, where I depend on a form

<form method='post' action='http://localhost/api/uploadAPI.php' enctype='multipart/form-data'><br>
<input type='file' name='foto' value='Cadastrar foto'>
<input type='submit' name='submit'>
</form>

This way, it works normally... The problem is that I depend on a form.

The mission is to implement so that I can send the file all via url more or less like this in send.php:

<?php
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'key: MEU_TOKEN'; 

$data = array(
    "foto"=> [ 
        "name"      => "nome_do_arquivo.jpeg",
        "type"      => "image/jpeg",
        "tmp_name"  => "CAMINHO_DO_ARQUIVO",
        "error"     => 0,
        "size"      => 82804,
    ]
);
$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL            => 'http://localhost/api/api.php?operacao=upload_img', 
    CURLOPT_HTTPHEADER     => $headers,
    CURLOPT_CUSTOMREQUEST  => 'POST',
    CURLOPT_POSTFIELDS     => json_encode($data),
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false
]);
$out = curl_exec($ch);
curl_close($ch);
print_r( $out ); // Result json

Using the form, I can recover the data in $_FILES['foto'] How can I send the file via send.php to retrieve it in the api with $_FILES['photo']?

1 answer

0

Have you tried implementing the $_GET method to send the information to an intermediate file treating the data, then rescue it and give the desired end to the data? Maybe it can work, since you want to pass the data through the URL.

  • 1

    I’m sorry, but I don’t think I understand what you said... Are you talking about creating a page to load with the information I want and submit the form? If so, still, I don’t know how to attach the file... When I recover the value of the field via jquery $('input['type=photo]'). val() I have filename.png, and the file is in C: Users Photos filename.png

Browser other questions tagged

You are not signed in. Login or sign up in order to post.