CURLFile::__construct

curl_file_create

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

CURLFile::__construct -- curl_file_createCreate a CURLFile object

Description

Object-oriented style

public CURLFile::__construct(string $filename, ?string $mime_type = null, ?string $posted_filename = null)

Procedural style

curl_file_create(string $filename, ?string $mime_type = null, ?string $posted_filename = null): CURLFile

Creates a CURLFile object, used to upload a file with CURLOPT_POSTFIELDS.

Parameters

filename

Path to the file which will be uploaded.

mime_type

Mimetype of the file.

posted_filename

Name of the file to be used in the upload data.

Return Values

Returns a CURLFile object.

Changelog

Version Description
8.0.0 mime_type and posted_filename are nullable now; previously their default was 0.

Examples

Example #1 CURLFile::__construct() example

Object-oriented style

<?php
/* http://example.com/upload.php:
<?php var_dump($_FILES); ?>
*/

// Create a cURL handle
$ch curl_init('http://example.com/upload.php');

// Create a CURLFile object
$cfile = new CURLFile('cats.jpg','image/jpeg','test_name');

// Assign POST data
$data = array('test_file' => $cfile);
curl_setopt($chCURLOPT_POST,1);
curl_setopt($chCURLOPT_POSTFIELDS$data);

// Execute the handle
curl_exec($ch);
?>

Procedural style

<?php
/* http://example.com/upload.php:
<?php var_dump($_FILES); ?>
*/

// Create a cURL handle
$ch curl_init('http://example.com/upload.php');

// Create a CURLFile object
$cfile curl_file_create('cats.jpg','image/jpeg','test_name');

// Assign POST data
$data = array('test_file' => $cfile);
curl_setopt($chCURLOPT_POST,1);
curl_setopt($chCURLOPT_POSTFIELDS$data);

// Execute the handle
curl_exec($ch);
?>

The above example will output:

array(1) {
  ["test_file"]=>
  array(5) {
    ["name"]=>
    string(9) "test_name"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/phpPC9Kbx"
    ["error"]=>
    int(0)
    ["size"]=>
    int(46334)
  }
}

Example #2 CURLFile::__construct() uploading multiple files example

Object-oriented style

<?php
$request 
curl_init('http://www.example.com/upload.php');
curl_setopt($requestCURLOPT_POSTtrue);
curl_setopt($requestCURLOPT_SAFE_UPLOADtrue);
curl_setopt($requestCURLOPT_POSTFIELDS, [
    
'blob[0]' => new CURLFile(realpath('first-file.jpg'), 'image/jpeg'),
    
'blob[1]' => new CURLFile(realpath('second-file.txt'), 'text/plain'),
    
'blob[2]' => new CURLFile(realpath('third-file.exe'), 'application/octet-stream'),
]);
curl_setopt($requestCURLOPT_RETURNTRANSFERtrue);

echo 
curl_exec($request);

var_dump(curl_getinfo($request));

curl_close($request);

Procedural style

<?php
// procedural
$request curl_init('http://www.example.com/upload.php');
curl_setopt($requestCURLOPT_POSTtrue); 
curl_setopt($requestCURLOPT_SAFE_UPLOADtrue);
curl_setopt($requestCURLOPT_POSTFIELDS, [
    
'blob[0]' => curl_file_create(realpath('first-file.jpg'), 'image/jpeg'),
    
'blob[1]' => curl_file_create(realpath('second-file.txt'), 'text/plain'),
    
'blob[2]' => curl_file_create(realpath('third-file.exe'), 'application/octet-stream'),
]);
curl_setopt($requestCURLOPT_RETURNTRANSFERtrue);

echo 
curl_exec($request);

var_dump(curl_getinfo($request));

curl_close($request);

The above example will output:

array(26) {
  ["url"]=>
  string(31) "http://www.example.com/upload.php"
  ["content_type"]=>
  string(24) "text/html; charset=UTF-8"
  ["http_code"]=>
  int(200)
  ["header_size"]=>
  int(198)
  ["request_size"]=>
  int(196)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.060062)
  ["namelookup_time"]=>
  float(0.028575)
  ["connect_time"]=>
  float(0.029011)
  ["pretransfer_time"]=>
  float(0.029121)
  ["size_upload"]=>
  float(3230730)
  ["size_download"]=>
  float(811)
  ["speed_download"]=>
  float(13516)
  ["speed_upload"]=>
  float(53845500)
  ["download_content_length"]=>
  float(811)
  ["upload_content_length"]=>
  float(3230730)
  ["starttransfer_time"]=>
  float(0.030355)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(0) ""
  ["primary_ip"]=>
  string(13) "0.0.0.0"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(80)
  ["local_ip"]=>
  string(12) "0.0.0.0"
  ["local_port"]=>
  int(34856)
}

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial