Sending MIME type files using cURL in PHP

Sending MIME type files using cURL in PHP

In this post we will show you Sending MIME type files using cURL in PHP, hear for Sending MIME type files using cURL in PHP we will give you demo and example for implement.

The cURL functions in PHP can be used to make HTTP requests to remote servers. The curl_setopt function allows a wide range of options to be set, but none of these directly relate to sending MIME type files. Instead, MIME type files are sent using a special format for POST parameter values.

how to send Sending MIME type files using cURL with PHP
how to send Sending MIME type files using cURL with PHP

The MIME type of the file sent in the cURL request can be specified in a similar way. Extending the previous example, the following code also sends the MIME type of the uploaded file:
// initialise the curl request
$request = curl_init('https://onlinecode.org/');

// send a file
curl_setopt($request, CURLOPT_POST, true);

curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' =>
          '@'            . $_FILES['files']['tmp_name']
          . ';filename=' . $_FILES['files']['name']
          . ';type='     . $_FILES['files']['type']
    ));
// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);

// close the session
curl_close($request);	

Sending simple type files using cURL in PHP

The following code makes a request to https://onlinecode.org/, uploads the file sample.txt, and outputs the response.

// initialise the curl request
$request = curl_init('https://onlinecode.org/');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('sample.txt')
    ));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);

// close the session
curl_close($request);

Hope this code and post will helped you for implement Sending MIME type files using cURL in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US