Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Dodanie tekstu do pliku w wybranym miejscu
Forum PHP.pl > Forum > PHP
AboutMe
Witam,

Chciałbym dodać adresy IP z jednego pliku php do .htaccess. Znalazłem taki tutorial ale coś nie działa - http://www.learncomputer.com/php-insert-te...le-at-position/

  1. <?php
  2.  
  3. function insert_into_file($file_path, $insert_marker,
  4. $text, $after = true) {
  5. $contents = file_get_contents($file_path);
  6. $new_contents = preg_replace($insert_marker,
  7. ($after) ? '$0' . $text : $text . '$0', $contents);
  8. return file_put_contents($file_path, $new_contents);
  9. }
  10.  
  11.  
  12. $file_path = ".htaccess";
  13. $insert_marker = "<Files *.php>";
  14. $text = file_get_contents('/home/username/public_html/blog/logs/IPs.php', true);
  15.  
  16. $num_bytes = insert_into_file($file_path, $insert_marker, $text, true);
  17.  
  18. if ($num_bytes === false) {
  19. echo "Could not insert into file $file_path.";
  20. } else {
  21. echo "Insert successful!";
  22. }
  23. ?>


Chciałbym dodać zawartość IPs.php do .htaccess zaraz za <Files *.php>

Kod zwraca Insert successful! ale .htaccess jest nietknięty.

Nawet takie coś nie działa:

  1. function insert_into_file($file_path, $insert_marker,
  2. $text, $after = true) {
  3. $contents = file_get_contents($file_path);
  4. $new_contents = preg_replace($insert_marker,
  5. ($after) ? '$0' . $text : $text . '$0', $contents);
  6. return file_put_contents($file_path, $new_contents);
  7. }
  8.  
  9.  
  10. $file_path = ".htaccess";
  11. $insert_marker = "<Files *.php>";
  12. $text = "0000000";
  13.  
  14. $num_bytes = insert_into_file($file_path, $insert_marker, $text, true);
  15.  
  16. if ($num_bytes === false) {
  17. echo "Could not insert into file $file_path.";
  18. } else {
  19. echo "Insert successful!";
  20. }
Wazniak96
Spróbuj tak smile.gif

  1. function insert_into_file($file_path, $insert_marker, $text, $after = true) {
  2. $contents = file_get_contents($file_path);
  3. $exp = explode($insert_marker, $contents);
  4. $exp[1] = " ".$text." ".$exp[1];
  5. $new_contents = implode($insert_marker, $contents);
  6. return file_put_contents($file_path, $new_contents);
  7. }
AboutMe
Dzięki ale zwraca mi taki błąd Warning: implode() [function.implode]: Invalid arguments passed tutaj:

  1. $new_contents = implode($insert_marker, $contents);


i usunęło mi wszystko z .htaccess
Wazniak96
Wybacz, mój błąd.
  1. $new_contents = implode($insert_marker, $exp); //łączymy w string tablicę,
AboutMe
Dzięku wielkie!
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.