CURLStringFile::__construct

(PHP 8 >= 8.1.0)

CURLStringFile::__constructCrea un objeto CURLStringFile

Descripción

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

Crea un objeto CURLStringFile, utilizado para subir un archivo con CURLOPT_POSTFIELDS.

Parámetros

data

Los contenidos a ser subidos.

postname

El nombre del archivo a ser utilizado en los datos subidos.

mime

El tipo MIME del archivo (por defecto es application/octet-stream).

Ejemplos

Ejemplo #1 Ejemplo con CURLStringFile::__construct()

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

// Crear un recurso cURL
$ch = curl_init('http://example.com/upload.php');

// Crear un objeto CURLStringFile
$cstringfile = new CURLStringFile('test upload contents','test.txt','text/plain');

// Asignar los datos POST
$data = array('test_string' => $cstringfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Ejecutar el recurso
curl_exec($ch);
?>

El resultado del ejemplo sería:

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"

Ver también

add a note

User Contributed Notes 1 note

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