PHP 8.1.28 Released!

CURLStringFile::__construct

(PHP 8 >= 8.1.0)

CURLStringFile::__constructCrée un objet CURLStringFile

Description

public CURLStringFile::__construct(string $data, string $postname, string $mime = "application/octet-stream")

Crée un objet CURLStringFile, utilisé pour télécharger un fichier avec CURLOPT_POSTFIELDS.

Liste de paramètres

data

Le contenu à télécharger.

postname

Le nom du fichier à utiliser dans les données à télécharger.

mime

Le type MIME du fichier (par défaut : application/octet-stream).

Exemples

Exemple #1 Exemple de CURLStringFile::__construct()

<?php
/* http://example.com/upload.php:
<?php
var_dump($_FILES);
var_dump(file_get_contents($_FILES['test_string']['tmp_name']));
?>
*/

// Crée un handle cURL
$ch = curl_init('http://example.com/upload.php');

// Crée un objet CURLStringFile
$cstringfile = new CURLStringFile('test upload contents','test.txt','text/plain');

// Assigne les data POST
$data = array('test_string' => $cstringfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

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

L'exemple ci-dessus va afficher :

array(1) {
  ["test_string"]=>
  array(5) {
    ["name"]=>
    string(8) "test.txt"
    ["type"]=>
    string(10) "text/plain"
    ["tmp_name"]=>
    string(14) "/tmp/phpTtaoCz"
    ["error"]=>
    int(0)
    ["size"]=>
    int(20)
  }
}
string(20) "test upload contents"

Voir aussi

add a note

User Contributed Notes 1 note

up
0
inmarket
11 months ago
Note that the $postname and $mimetype parameters are in the reverse order to the CURLFile::__construct function.
To Top