0

i have a form which has a file input like below, i'd like to upload the file selected by user(on their device) to another server.

    <form id="image" method="post" action="/api">
    <input name="image" type="file">
    <input type="submit" value="submit">
    </form>

i have found some answers here and there but they don't seem to work or maybe i can not understand. for example, i have encountered

$post = array (
    'image' => curl_file_create('path/to/image');
);

or new CURLFile('path/to/image'); but i don't figure out what 'path/to/image' is since the image(file) is not saved on the server!

i'd appreciate any help, thanks in advance!

p.s. the form has other inputs as well and the curl sends their data without any problem, the problem is just the file input. just cut out those inputs so the code wouldn't be long.

KoCo
  • 1
  • 1
  • There are two pieces to uploading a file, 1. the _sending_ script, 2. the _receiving_ script. Currently, you are only showing us a piece of the sending script. You also need the receiving script to handle the incoming file. Have you coded that piece yet? If so, please share it with us. Also, what version of PHP are you running? PHP 8 introduced _CURLStringFile_ which allows you to upload the _contents_ of the file, as opposed to the path of the file https://kinsta.com/blog/php-8-1/#file-uploads-from-strings-with-curlstringfile. Not sure if that applies to you though. – waterloomatt Jan 21 '22 at 14:00
  • i can handle $_FILES in php if its directly sent. my problem is with cURL. the receiving piece varies from case to case and works just fine if the form is sent directly . i am currently using php v7.4 but it can be upgraded to 8.1. thanks – KoCo Jan 21 '22 at 14:06
  • The user must first upload the file to your server. That will happen when you submit the form. So when the PHP code in `/api` gets executed, the file is already on your server. So in that script, you can upload the file to another server. You can find the path to the uploaded file in `$_FILES['image']['tmp_name']` (it gets uploaded to a temp file) and the filename in `$_FILES['image']['name']`. – M. Eriksson Jan 21 '22 at 14:27
  • so... it should be `curl_file_create($_FILES['image']['tmp_name'].$_FILES['image']['name']);`?! – KoCo Jan 21 '22 at 14:38
  • @KoCo — No. Don't concatenate the name to the full path. The full path already includes the name. And you need to upload the file in the first place (see the duplicate). – Quentin Jan 21 '22 at 15:29

0 Answers0