1

I'm trying to send a file using curl and PHP on anonfile but I get this json:

{"status":false,"error":{"message":"No file chosen.","type":"ERROR_FILE_NOT_PROVIDED","code":10}}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://anonfile.com/api/upload");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,'test.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
print_r($server_output);
curl_close ($ch);

In other words, how to translate this command into PHP?

curl -F "file=@test.txt" https://anonfile.com/api/upload

I tried several examples out there but still no clue

$target_url = 'https://anonfile.com/api/upload';
$args['file'] = '@/test.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
Diok
  • 83
  • 2
  • 10
  • 3
    Possible duplicate of [how to upload file using curl with php](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – Satish Saini Feb 02 '19 at 18:19
  • @SatishSaini the answer provided in that duplicate won't work tho (not since PHP 5.6, anyway) – hanshenrik Feb 02 '19 at 20:22

2 Answers2

1
curl_setopt($ch, CURLOPT_POSTFIELDS,'test.txt');

won't work because it's literally just sending the literal string test.txt

$args['file'] = '@/test.txt';

won't work because the @ prefix to upload files was deprecated in PHP 5.5, disabled-by-default in PHP 5.6, and completely removed in PHP 7.0. in PHP 5.5 and higher, use CURLFile to upload files in the multipart/form-data format.

as of PHP 5.5+ (which is ancient at this point),

curl -F "file=@test.txt" https://anonfile.com/api/upload

translates to

$ch=curl_init();
curl_setopt_array($ch,array(
    CURLOPT_URL=>'https://anonfile.com/api/upload',
    CURLOPT_POST=>1,
    CURLOPT_POSTFIELDS=>array(
        'file'=>new CURLFile("test.txt")
    )
));
curl_exec($ch);
curl_close($ch);
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
0
$request = curl_init('https://api.anonfiles.com/upload');
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, 
CURLOPT_SAFE_UPLOAD, true); curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    [
        'file' => new CURLFile(realpath('test.txt'), 'text/plain'),
    ] );
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

echo curl_exec($request);

var_dump(curl_getinfo($request));

curl_close($request);
Danish
  • 1