Hướng dẫn curlfile in php - curlfile trong php

Có rất nhiều giải pháp để upload file lên server cho PHP như sử dụng form, ajax, ftp, scp, … Hôm nay vinasupport sẽ hướng dẫn các bạn sử dụng curl để upload file tới server sử dụng php.

Để thực hiện tutorial này chúng ta cần 3 file sau:

  • curl.php: Xử lý script curl để đọc và xử lý upload file tới server (client)
  • upload.php: Xử lý file nhận từ client (server)
  • data.txt: File dữ liệu

Xử lý upload file tới server

File: curl.php

 curl_file_create('data.txt'),
    'description' => 'Upload file to server by url (vinasupport.com)'
];

// Set CURL options
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://localhost/upload.php',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_VERBOSE => 1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $fields,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false
));

//create the multiple cURL handle
$mh = curl_multi_init();

//add the handle
curl_multi_add_handle($mh, $curl);

//execute the handle
do {
    $status = curl_multi_exec($mh, $active);
    if ($active) {
        curl_multi_select($mh);
    }
} while ($active && $status == CURLM_OK);

//close the handles
curl_multi_remove_handle($mh, $curl);
curl_multi_close($mh);

// all of our requests are done, we can now access the results
echo curl_multi_getcontent($curl);

Trường hợp bạn thực hiện trên local thì nên set 2 option là CURLOPT_SSL_VERIFYHOST  và CURLOPT_SSL_VERIFYPEER  về false để tránh xác thực certificate.CURLOPT_SSL_VERIFYHOST  và CURLOPT_SSL_VERIFYPEER  về false để tránh xác thực certificate.

Xử lý file nhận từ client

File: upload.php

Ở đây mình chỉ hiển thị dữ liệu gửi lên từ client, còn các bạn có thể tùy ý xử lý dữ liệu.

Kết quả:

Hướng dẫn curlfile in php - curlfile trong php

Nguồn: vinasupport.com

alin dot rzv at gmail dot com ¶

8 years ago

i've seen some downvotes , here is a small example of using curl to upload image .

$target="http://youraddress.tld/example/upload.php";# http://php.net/manual/en/curlfile.construct.php

// Create a CURLFile object / procedural method

$cfile = curl_file_create('resource/test.png','image/png','testpic'); // try adding

// Create a CURLFile object / oop method
#$cfile = new CURLFile('resource/test.png','image/png','testpic'); // uncomment and use if the upper procedural method is not working.

// Assign POST data

$imgdata = array('myimage' => $cfile);$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://someaddress.tld','Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true); // enable posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); // post images
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
$r = curl_exec($curl);
curl_close($curl);?>

CertaiN ¶

8 years ago

There are "@" issue on multipart POST requests.

Solution for PHP 5.5 or later:
- Enable CURLOPT_SAFE_UPLOAD.
- Use CURLFile instead of "@".

Solution for PHP 5.4 or earlier:
- Build up multipart content body by youself.
- Change "Content-Type" header by yourself.

The following snippet will help you :D

0

1

CertaiN ¶

php at miknik dot co dot uk ¶

2

3

4

5

1

4 years ago

php at miknik dot co dot uk ¶

7

8

9

i've seen some downvotes , here is a small example of using curl to upload image .0

i've seen some downvotes , here is a small example of using curl to upload image .1

i've seen some downvotes , here is a small example of using curl to upload image .2

i've seen some downvotes , here is a small example of using curl to upload image .3

i've seen some downvotes , here is a small example of using curl to upload image .4

1

4 years ago

ohcc at 163 dot com ¶

i've seen some downvotes , here is a small example of using curl to upload image .6

i've seen some downvotes , here is a small example of using curl to upload image .7

1