CURLFile::__construct

curl_file_create

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

CURLFile::__construct -- curl_file_createErstellt ein CURLFile-Objekt

Beschreibung

Objektorientierter Stil

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

Prozeduraler Stil

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

Erstellt ein CURLFile, welches zum Upload einer Datei via CURLOPT_POSTFIELDS verwendet wird.

Parameter-Liste

filename

Der Pfad zur Datei, die hochgeladen wird.

mime_type

Der Mimetyp der Datei.

posted_filename

Der in den Upload-Daten zu verwendende Dateiname.

Rückgabewerte

Gibt ein CURLFile-Objekt zurück.

Changelog

Version Beschreibung
8.0.0 mime_type und posted_filename sind jetzt nullable (akzeptieren den null-Wert); vorher war ihr Vorgabewert 0.

Beispiele

Beispiel #1 CURLFile::__construct()-Beispiel

Objektorientierter Stil

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

// Erstellt ein cURL-Handle
$ch curl_init('http://example.com/upload.php');

// Erstellt ein CURLFile-Objekt
$cfile = new CURLFile('cats.jpg','image/jpeg','test_name');

// POST-Daten hinzufügen
$data = array('test_file' => $cfile);
curl_setopt($chCURLOPT_POST,1);
curl_setopt($chCURLOPT_POSTFIELDS$data);

// Das Handle ausführen
curl_exec($ch);
?>

Prozeduraler Stil

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

// Erstellt ein cURL-Handle
$ch curl_init('http://example.com/upload.php');

// Erstellt ein CURLFile-Objekt
$cfile curl_file_create('cats.jpg','image/jpeg','test_name');

// POST-Daten hinzufügen
$data = array('test_file' => $cfile);
curl_setopt($chCURLOPT_POST,1);
curl_setopt($chCURLOPT_POSTFIELDS$data);

// Das Handle ausführen
curl_exec($ch);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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)
  }
}

Beispiel #2 CURLFile::__construct()-Beispiel zum Upload mehrerer Dateien

Objektorientierter Stil

<?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);

Prozeduraler Stil

<?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]' => 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);

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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)
}

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

SELL FULLSsn Dob DL CS CC CVV US UK AUS CA EU ASIA INTER,PASS VBV/BIN/DOB1

VERIFIED CARDER SELLING WU,BANK,PAYPAL,CASHAPP,SKRILL TRANSFER BANK LOGS,DUMPS+PIN,CLONED CARDS​ SELL CCV CANADA FULLZ FRESH SSN DOB WITH DL LI ...

Geschrieben von sellergood11 am 19.05.2026 13:55:53
Forum: Netzwerk &amp; Internet
@king_dumps12 Sell Valid CVV Dumps With Pin, Buy Dumps CVV Online, Legit CVV Shop

*************************** I'm good hacker and verified seller Having experience in information technology in 9 years I need best buyer and long ...

Geschrieben von sellergood11 am 19.05.2026 13:53:07
Forum: Windows (Client/Server)
telegram:@king_dumps12 SELL CC CVV CVV2 DUMPS12 WITH PIN FULL 2026 ALL COUNTRY

HOT Seller CVV Good 2026 - NON VBV Credit Card/Debit Card U.P.D.A.T.E CVV 2026 Sell CVV Good info And High Balance (Cvv CC Fullz Credit Cards D ...

Geschrieben von sellergood11 am 19.05.2026 13:51:33
Forum: Grafik / Design / Flash ...
@king_dumps12 Sell cvv US-UK-AUS-CA-EU-ASIA-dump12 2026 FULL Ssn CS Dob DL fresh

HOT Seller CVV Good 2026 - NON VBV Credit Card/Debit Card U.P.D.A.T.E CVV 2026 Sell CVV Good info And High Balance (Cvv CC Fullz Credit Cards Du ...

Geschrieben von sellergood11 am 19.05.2026 13:50:17
Forum: SEO - Suchmaschinen Tricks und Tipps