PHP 8.3.4 Released!

Exemples

Sommaire

Exemple #1 Exemples avec file_get_contents()

<?php
/* Lit un fichier local dans le dossier /home/bar */
$localfile = file_get_contents("/home/bar/foo.txt");

/* Identique au précédent, mais utilise explicitement le gestionnaire FILE */
$localfile = file_get_contents("file:///home/bar/foo.txt");

/* Lit un fichier distant sur le serveur www.example.com avec le protocole HTTP */
$httpfile = file_get_contents("http://www.example.com/foo.txt");

/* Lit le même fichier sur le serveur www.example.com avec le protocole HTTPS */
$httpsfile = file_get_contents("https://www.example.com/foo.txt");

/* Lit un fichier distant sur le serveur ftp.example.com en utilisant le protocole FTP */
$ftpfile = file_get_contents("ftp://user:pass@ftp.example.com/foo.txt");

/* Lit un fichier distant sur le serveur ftp.example.com en utilisant le protocole FTPS */
$ftpsfile = file_get_contents("ftps://user:pass@ftp.example.com/foo.txt");
?>

Exemple #2 Envoi d'une requête de type POST sur un serveur sécurisé

<?php
/* Envoi d'une requête POST sur le serveur https://secure.example.com/form_action.php
* Inclusion des variables "foo" et "bar"
*/

$sock = fsockopen("ssl://secure.example.com", 443, $errno, $errstr, 30);
if (!
$sock) die("$errstr ($errno)\n");

$data = "foo=" . urlencode("Value for Foo") . "&bar=" . urlencode("Value for Bar");

fwrite($sock, "POST /form_action.php HTTP/1.0\r\n");
fwrite($sock, "Host: secure.example.com\r\n");
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, $data);

$headers = "";
while (
$str = trim(fgets($sock, 4096)))
$headers .= "$str\n";

echo
"\n";

$body = "";
while (!
feof($sock))
$body .= fgets($sock, 4096);

fclose($sock);
?>

Exemple #3 Écrire des données dans un fichier compressé

<?php
/* Création d'un fichier compressé contenant une chaîne arbitraire
* Le fichier peut être lu en utilisant le gestionnaire compress.zlib
* ou simplement decompresse; en ligne de commande avec 'gzip -d foo-bar.txt.gz'
*/
$fp = fopen("compress.zlib://foo-bar.txt.gz","w");
if (!
$fp) die("Impossible de créer le fichier.");

fwrite($fp, "Ceci est un test.\n");

fclose($fp);
?>

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top