Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Algorytm] Zapisz do pliku
Forum PHP.pl > Forum > Gotowe rozwiązania > Algorytmy, klasy, funkcje
Spirit86
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
Bakus
1. "file_put_content" nie będzie działać nikomu, puki nie napisze sobie samemu tej funkcji... literówka... file_pub_contents
2. Lepiej było nazwać funkcję 'file_pub_contents' i umieścić ją w funkcji warunkowej:
  1. <?php
  2. if (!function_exists('file_put_contents'))
  3. {
  4. // Twoja definicja funkcji
  5. }
  6. ?>

Dzięki temu można używać tej samej nazwy funkcji pomimo wykorzystania PHP4.
AxZx
ty tez zrobiles kilka literowek przez co twoj post jest troszke niezrozumialy. przynajmniej dla mnie.
Spirit86
jak Cie to tak ucieszy, to masz tongue.gif
  1. <?php
  2. define('DEBUGMODE', true); //Debug mode on - true, off - false 
  3. if (!function_exists('file_put_contents'))
  4. {
  5. function file_put_contents($filename, $what)
  6. {
  7. if (!file_exists($filename))
  8. {
  9.  if(!touch ($filename))
  10.  {
  11.  error('Cannot Create File');
  12.  return false;
  13.  }
  14. }
  15. if (is_writable($filename))
  16. {
  17.  if ($handle = fopen($filename, 'r+'))
  18.  {
  19. if (fwrite($handle, $what))
  20. {
  21. return true;
  22. }
  23. else
  24. {
  25. error('Cannot write '.$what.' into '.$filename);
  26. return false;
  27. }
  28. fclose($uchwyt);
  29.  }
  30.  else
  31.  {
  32.  error('Cannot create Handle for '.$filename);
  33.  return false;
  34.  }
  35.  
  36. }
  37. else
  38. {
  39. error('File is not Writeable');
  40. return false;
  41. }
  42.  
  43. }
  44. function error($text)
  45. {
  46. if(DEBUGMODE)
  47. {
  48. echo '<center><h3 style="color: red;">'.$text.'</h3></center>';
  49. }
  50. }
  51. }
  52. ?>


dla mnie to może się nazywać zapisz_plik() i tylko pod taką nazwą bym tego używał. Ale jak kto woli smile.gif.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.