Manuel PHP

Exemples

Exemple #1 Exemples avec file_get_contents()

  1. <?php
  2. /* Lit un fichier local dans le dossier /home/bar */  
  3. $localfile = file_get_contents("/home/bar/foo.txt");  
  4.  
  5. /* Identique au précédent, mais utilise explicitement le gestionnaire FILE */  
  6. $localfile = file_get_contents("file:///home/bar/foo.txt");  
  7.  
  8. /* Lit un fichier distant sur le serveur www.example.com avec le protocole HTTP */  
  9. $httpfile = file_get_contents("http://www.example.com/foo.txt");  
  10.  
  11. /* Lit le même fichier sur le serveur www.example.com avec le protocole HTTPS */  
  12. $httpsfile = file_get_contents("https://www.example.com/foo.txt");  
  13.  
  14. /* Lit un fichier distant sur le serveur ftp.example.com en utilisant le protocole FTP */  
  15. $ftpfile = file_get_contents("ftp://user:pass@ftp.example.com/foo.txt");  
  16.  
  17. /* Lit un fichier distant sur le serveur ftp.example.com en utilisant le protocole FTPS */  
  18. $ftpsfile = file_get_contents("ftps://user:pass@ftp.example.com/foo.txt");  
  19. ?> 

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

  1. <?php
  2. /* Envoi d'une requête POST sur le serveur https://secure.example.com/form_action.php
  3. * Inclusion des variables "foo" et "bar"
  4. */  
  5.  
  6. $sock = fsockopen("ssl://secure.example.com", 443, $errno, $errstr, 30);  
  7. if (!$sock) die("$errstr($errno)\n");  
  8.  
  9. $data = "foo=" . urlencode("Valeur de Foo") . "&bar=" . urlencode("Valeur de Bar");  
  10.  
  11. fputs($sock, "POST /form_action.php HTTP/1.0\r\n");  
  12. fputs($sock, "Host: secure.example.com\r\n");  
  13. fputs($sock, "Content-type: application/x-www-form-urlencoded\r\n");  
  14. fputs($sock, "Content-length: " . strlen($data) . "\r\n");  
  15. fputs($sock, "Accept: */*\r\n");  
  16. fputs($sock, "\r\n");  
  17. fputs($sock, "$data\r\n");  
  18. fputs($sock, "\r\n");  
  19.  
  20. $headers = "";  
  21. while ($str = trim(fgets($sock, 4096))) {  
  22. $headers .= "$str\n";  
  23. }  
  24.  
  25. echo "\n";  
  26.  
  27. $body = "";  
  28. while (!feof($sock)) {  
  29. $body .= fgets($sock, 4096);  
  30. }  
  31.  
  32. fclose($sock);  
  33. ?> 

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

  1. <?php
  2. /* Création d'un fichier compressé contenant une chaîne arbitraire
  3. * Le fichier peut être lu en utilisant le gestionnaire compress.zlib
  4. * ou simplement decompresse; en ligne de commande avec 'gzip -d foo-bar.txt.gz'
  5. */  
  6. $fp = fopen("compress.zlib://foo-bar.txt.gz","w");  
  7. if (!$fp) die("Impossible de créer le fichier.");  
  8.  
  9. fwrite($fp, "Ceci est un test.\n");  
  10.  
  11. fclose($fp);  
  12. ?> 


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique