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
 
Start new topic
Odpowiedzi (1 - 3)
Bakus
post
Post #2


Administrator serwera


Grupa: Przyjaciele php.pl
Postów: 909
Pomógł: 0
Dołączył: 12.08.2003
Skąd: /var/www/wroclaw.php

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


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.


--------------------
Powrót do przeszłości :)
Go to the top of the page
+Quote Post
AxZx
post
Post #3





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 55
Dołączył: 1.03.2005
Skąd: śląsk

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


ty tez zrobiles kilka literowek przez co twoj post jest troszke niezrozumialy. przynajmniej dla mnie.


--------------------
aplikacje internetowe | Symfony
Go to the top of the page
+Quote Post
Spirit86
post
Post #4





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

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


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.


--------------------
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

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: 22.08.2025 - 03:56