info:myphp:fopen

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
info:myphp:fopen [2024/08/06 08:40] radeffinfo:myphp:fopen [2026/03/05 09:28] (Version actuelle) radeff
Ligne 1: Ligne 1:
-Voir aussi:+====== PHP fopen, fwrite ======
  
-[[fgestcsv]] pour faire du fopen sur du csv+Différentes approches dans la manipulation de fichiers avec PHP
  
-[[array]] plus généralement la fonction array, avec un exemple pour ouvrir un fichier csv+===== fopen =====
  
 <code php> <code php>
-   if (!$handle = fopen($filename, 'a')) { 
-echo "erreur..."; 
-exit; 
-} 
-</php> 
- 
-<code> 
-Using fread () in PHP http://php.about.com/od/advancedphp/ss/php_read_file_2.htm 
- 
-You can use fread () when you want to read the file data and return it to a string. You can specify how many bytes to read, but 8192 is the maximum. If you choose to use this method, you must be sure to open the file first. 
- 
     <?php     <?php
     $YourFile = "YourFile.txt";     $YourFile = "YourFile.txt";
Ligne 24: Ligne 13:
     print $Data;     print $Data;
     ?>      ?> 
 +</code>
  
-This code opens the file YourFile.txt from your server and puts it's entire contents into the variable $Data as a string. We then print $Data, so the script is basically outputting the file contents onto a page.. +==== File_get_contents  ==== 
- +<code php>
- +
-Using file_get_contents () in PHP http://php.about.com/od/advancedphp/ss/php_read_file_3.htm +
- +
-File_get_contents () is similar to fread () in that it returns the entire contents of the file to a single string, however it can be done in a single line and performs better then fread (). +
     <?php     <?php
     $file = file_get_contents ('YourFile.txt');     $file = file_get_contents ('YourFile.txt');
     Echo $file;     Echo $file;
     ?>      ?> 
 +</code>
  
-This little bit of code retrieves the data from YourFile.text and then echos it onto the page. 
  
  
-Using fgets () in PHP http://php.about.com/od/advancedphp/ss/php_read_file_4.htm +==== fgets ====
- +
-Fgets () is used to read the data from a file one line at a time starting from the pointer. It defaults to only reading the first 1024 bytes of a line, however you can set this variable higher or lower if you wish. If your file is not separated with line breaks, this is not the right function to use.+
  
 +<code php>
     <?php     <?php
     $YourFile = "YourFile.txt";     $YourFile = "YourFile.txt";
Ligne 55: Ligne 39:
     fclose($handle);     fclose($handle);
     ?>      ?> 
 +</code>
  
-What this code does first is open the file YourFile.txt. It then enters into a loop that will read up to 256 bytes of the file line, print the contents of the line, print an HTML paragraph break, and then repeat this with the next line until it reaches the end of the file (foef). +==== File ====
- +
-Using file () in PHP http://php.about.com/od/advancedphp/ss/php_read_file_5.htm +
- +
-File () is similar to fgets in that it reads the data one line at a time, however it returns it all at once into an array. The array contains the line number, and the data corresponding to each line number starting with 0. If you want to do more than simply echo the data, having it in an array will provide much more flexibility making file () more useful than fgets ().+
  
 +<code php>
     <?php     <?php
     $lines = file('YourFile.txt');     $lines = file('YourFile.txt');
Ligne 69: Ligne 51:
     }     }
     ?>      ?> 
 +</code>
 What this code does first is put the contents of YourFile.txt into the array called $lines. What this code does first is put the contents of YourFile.txt into the array called $lines.
 +
 +
 +===== fopen-fwrite-fput =====
 +<code>
 +<?php
 +$filename = 'test.txt';
 +$somecontent = "Ajout de chaîne dans le fichier \n";
 +
 +// Assurons nous que le fichier est accessible en écriture
 +if (is_writable($filename)) {
 +
 +   // Dans notre exemple, nous ouvrons le fichier $filename en mode d'ajout
 +   // Le pointeur de fichier est placé à la fin du fichier
 +   // c'est là que $somecontent sera placé
 +   if (!$handle = fopen($filename, 'a')) {
 +         echo "Impossible d'ouvrir le fichier ($filename)";
 +         exit;
 +   }
 +
 +   // Ecrivons quelque chose dans notre fichier.
 +   if (fwrite($handle, $somecontent) === FALSE) {
 +       echo "Impossible d'écrire dans le fichier ($filename)";
 +       exit;
 +   }
 +  
 +   echo "L'écriture de ($somecontent) dans le <a href=$filename>fichier ($filename)</a> a réussi";
 +  
 +   fclose($handle);
 +                  
 +} else {
 +   echo "Le fichier $filename n'est pas accessible en écriture.";
 +}
 +?>
 </code> </code>
 +
 +===== fopen htaccess page =====
 +Si votre serveur utilise un fichier .htaccess pour authentifier les utilisateurs, assurez-vous d'ajouter le nom d'utilisateur et le mot de passe au lien HTTP que vous essayez d'ouvrir. 
 +
 +<code>
 +fopen("http://user:pass@www.mysite.com/mypage.php");
 +</code>
 +
 +
 +===== PHP allow_url_fopen / allow_url_include =====
 +Ce procédé est aujourd'hui utilisé pratiquement à 100% pour des tentatives de piratages ou de phishing et n'est que rarement nécessaire.
 +
 +il est possible de les activer si nécessaire en introduisant l'une des valeurs suivantes dans un fichier .htaccess :
 +
 +<code bash>
 +php_flag "allow_url_fopen" "On"
 +php_flag "allow_url_include" "On"
 +</code>
 +
 +===== effacer =====
 +
 +<code php>
 +<?php
 +/* [[effacer.php]] Script to remove files / directories on a web server *** CAREFULL!!!
 +#
 +# Authors:
 +# FR, radeff@akademia.ch
 +# History
 +#  --- //[[fradeff@akademia.ch|Fred Radeff]] 2008/02/01 15:03//: created;
 +#########
 +*/
 +
 +function SureRemoveDir($dir, $DeleteMe) {
 +    if(!$dh = @opendir($dir)) return;
 +    while (false !== ($obj = readdir($dh))) {
 +        if($obj=='.' || $obj=='..') continue;
 +        if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
 +    }
 +
 +    closedir($dh);
 +    if ($DeleteMe){
 +        @rmdir($dir);
 +    }
 +}
 +function delfile($str)
 +{
 +    foreach(glob($str) as $fn) {
 +        unlink($fn);
 +    }
 +
 +#SureRemoveDir('EmptyMe', false);
 +#SureRemoveDir('RemoveMe', true);
 +#delfile("equipe/fr*");
 +rmdir("xmedia");
 +?>
 +</code>
 +
 +
 +===== Voir aussi =====
 +  * [[fgestcsv]] pour faire du fopen sur du csv
 +  * [[array]] plus généralement la fonction array, avec un exemple pour ouvrir un fichier csv
 +
  
  
  • info/myphp/fopen.1722926439.txt.gz
  • Dernière modification : 2024/08/06 08:40
  • de radeff