Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Algorytm] Zapisz do pliku
Spirit86
post
Post #1





Grupa: Zarejestrowani
Postów: 607
Pomógł: 23
Dołączył: 8.09.2004
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


Witam. Napisałem prostą funkcję zapisywania treści do pliku, jak komuś nie działa np. file_put_content" title="Zobacz w manualu php" target="_manual może się przydać.
  1. <?php
  2.  
  3. define('DEBUGMODE', true); //Debug mode on - true, off - false 
  4. function savetofile($filename, $what)
  5. {
  6. if (!file_exists($filename))
  7. {
  8.  if(!touch ($filename))
  9.  {
  10.  error('Cannot Create File');
  11.  return false;
  12.  }
  13. }
  14. if (is_writable($filename))
  15. {
  16.  if ($handle = fopen($filename, 'r+'))
  17.  {
  18. if (fwrite($handle, $what))
  19. {
  20. return true;
  21. }
  22. else
  23. {
  24. error('Cannot write '.$what.' into '.$filename);
  25. return false;
  26. }
  27. fclose($uchwyt);
  28.  }
  29.  else
  30.  {
  31.  error('Cannot create Handle for '.$filename);
  32.  return false;
  33.  }
  34.  
  35. }
  36. else
  37. {
  38. error('File is not Writeable');
  39. return false;
  40. }
  41.  
  42. }
  43. function error($text)
  44. {
  45.   if(DEBUGMODE)
  46.   {
  47.     echo '<center><h3 style="color: red;">'.$text.'</h3></center>';
  48.     EXIT;
  49.   }
  50. }
  51.  
  52. ?>

Sposób użycia:
  1. <?php
  2.  
  3. $text = 'options = 1;
  4. show=4';
  5. savetofile('./plik.ini', $text);
  6. // lub gdy mamy wyłączony DEBUGMODE if(savetofile('./plik.ini', $text)) ...
  7.  
  8. ?>

Pozdrawiam

Ten post edytował Spirit86 9.12.2005, 14:37:07


--------------------
Audio: Metallica, Soil, RHCP, OffSpring, Green Day, "Retro", Gorillaz, Disturbed, Coma
DB: MySQL 4.1 | php: 4.4.3 Pomogłem Ci? Wciśnij przycisk POMÓGŁ.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 10:27