简体   繁体   中英

How to send file with GET and not POST with curl in php

I'm trying to send my file, but using GET not POST with curl.

I have:

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "http://localhost/test.php/?file_contents=$file");

    $result = curl_exec($ch);

I know that I can easily send the file with a POST curl request like so (taken from this question here ):

$data = array(
'uploaded_file' => '@'.$tmpfile.';filename='.$filename,
);

$ch = curl_init();   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

However, I don't want my request to be a POST. I can't seem to figure out how to incorporate a file transfer without it.

Any help or recommendations? I thought of using curl_file_create() but I'm not sure how to implement it in this as the documentation for it is rather terrible.

curl_setopt($ch, CURLOPT_URL, "http://localhost/test.php/?".http_build_query(array(
    'name'=>basename($file),
    'file_contents'=>file_get_contents($file)
)));

urlencoding is binary safe, so you can send any file contents this way, however, for big files, url encoding is very wasteful compared to multipart/form-data, the overhead per non-ascii byte (and even some ascii bytes, like 0x20) is 300% (for example, a NULL byte is encoded as %00 , using 3 bytes, where multipart/form-data would only use 1 byte. a 10 MB binary file upload becomes ~30MB , where multipart/form-data would use 10 MB) - so don't do this if you're uploading big files, or if bandwidth is expensive. you should also keep in mind the very limted max url length limitation present by default in many popular web servers (like nginx and apache), and how most web servers log the full url, so your access log files will grow very large, containing every file uploaded this way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM