Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Edytor plików w php - znowu mi coś niedziała :/
Forum PHP.pl > Forum > Przedszkole
RoxyFox
W książce* jest taki skrypt do edytowania plików tekstowych. Przepisałem go żywcem i nie działa. Ja już nie mogę z tej książki... Co chwile jest jakiś drobny błąd. A że ja jestem początkujący to go niewidzę sad.gif. Ale bardzo dobrze są opisane inne rzeczy. No więc tak:



To się składa z 2 plików. W jednym są tylko funkcje.

common.inc
  1. <?php
  2. $default_dir = "/docs";
  3. $default_filename = "new.txt";
  4. $edit_form_cols = 80;
  5. $edit_form_rows = 25;
  6. $text_file_array = array ("txt", "htm", "html", "php", "inc", "dat" );
  7. $image_file_array = array("gif", "jpeg", "jpg", "png");
  8.  
  9. function html_header() {
  10. echo "<html>";
  11. echo "<head>";
  12. echo "</head>";
  13. echo "<title>Witamy w edytorze tekstu</title>";
  14. echo "</head>";
  15. echo "<body>";
  16. }
  17.  
  18. function html_footer() {
  19. echo "</body>";
  20. echo "</html>";
  21. }
  22.  
  23. function error_message($msg) {
  24. html_header();
  25. echo "<script>alert(\"$msg\"); history.go(-1)</SCRIPT>";
  26. html_footer();
  27. }
  28.  
  29. function date_str($timestamp) {
  30. $date_str = getdate($timestamp);
  31. $year = $date_str["year"];
  32. $mon = $date_str["mon"];
  33. $mday = $date_str["mday"];
  34. $hours = $date_str["hours"];
  35. $minutes = $date_str["minutes"];
  36. $seconds = $date_str["seconds"];
  37. if ($mon < 10) $mon = "0$mon";
  38. if ($mday <10) $mday = "0$mday";
  39. if ($hours <10) $hours = "0$hours";
  40. if ($minutes <10) $minutes = "0$minutes";
  41. if ($seconds <10) $seconds = "0$seconds";
  42. return "$hours:$minutes:$seconds $mday/$mon/$year";
  43. }
  44.  
  45. function file_info($file) {
  46. global $text_file_array, $WINDIR;
  47.  
  48. $file_info_array["filesize"] = number_format(filesize($file)) . " bytes.";
  49. $file_info_array["filectime"] = date_str(filectime($file));
  50. $file_info_array["filemtime"] = date_str(filemtime($file));
  51.  
  52. if (!isset($WINDIR)) {
  53. $file_info_array["fileatime"] = date_str(fileatime($file));
  54. $file_info_array["filegroup"] = filegroup($file);
  55. $file_info_array["fileowner"] = fileowner($file);
  56. } else {
  57. $file_info_array["fileatime"] = "not available";
  58. $file_info_array["filegroup"] = "not available";
  59. $file_info_array["fileowner"] = "not available";
  60. }
  61.  
  62. $extension = array_pop(explode(".", $file));
  63. if (in_array($extension, $text_file_array)) $file_info_array["filetype"] = "text";
  64. else $file_info_array["filetype"] = "binary";
  65. return $file_info_array;
  66. }
  67.  
  68.  
  69.  
  70. ?>


editor.php
  1. <?php
  2. include "common.inc";
  3.  
  4. function editor_form($dir, $filename, $is_new) {
  5.  global $PHP_SELF, $edit_form_cols, $edit_form_rows;
  6.  
  7.  $filepath = "$dir/$filename";
  8.  if(!$is_new) $filebody = implode("", file($filepath));
  9.  $file_info_array = file_info("$filepath");
  10.  $editable = 1;
  11.  if($file_info_array["filetype"] != "text") {
  12. $filebody = $filepath . " nie jest plikiem tekstowym.
  13.  Nie powinienes go edytowac.";
  14. $editable = 0;
  15.  }
  16.  if($editable) {
  17. ?>
  18. <center>
  19. <form name="edit_form" method="post" action="<?php echo $PHP_SELF ?>">
  20. <input type="hidden" name="action" value="save_file" />
  21. <input type="hidden" name="dir" value="<?php echo "$dir" ?>" />
  22. <TEXTAREA rows="<?php echo $edit_form_rows ?>" name="filebody" 
  23. cols="<?php echo $edit_form_cols ?>" wrap="soft">
  24. <?php echo "$filebody"; ?>
  25. </TEXTAREA><br />
  26. Nazwa pliku: <?php echo "<strong>$dir/</strong>"; ?>
  27. <input type="text" name="filename" value="<?php echo $filename ?>"
  28. size="30" />
  29. <input type="submit" value="Zapisz" name="submit" />
  30. </form>
  31. </center>
  32.  
  33. <?php
  34.  }
  35.  else {
  36. echo "<center><strong><font color='red'>$filebody</font></strong></center>\n";
  37.  }
  38. }
  39.  
  40. function edit_new_form() {
  41.  global $PHP_SELF, $default_dir, $dir;
  42. ?>
  43.  
  44. <center><form method="post" action="<?php echo $PHP_SELF ?>">
  45. <input type="hidden" name="action" value="editor_page" />
  46. <input type="hidden" name="dir" value="<?php echo $dir ?>" />
  47. <input type="submit" value="Nowy" /></form></center>
  48.  
  49. <?php
  50. }
  51.  
  52. function save_file() {
  53.  global $filename, $filebody, $dir, $PHP_SELF;
  54.  if(file_exists("$dir/$filename")) {
  55. echo "<script>result =
  56.  confirm(\"Nadpisac '$dir/$filename' ?\");
  57.  if (!result) history.go (-1);</SCRIPT>";
  58.  }
  59.  if($file = fopen("$dir/$filename", "w")) {
  60. fputs($file, $filebody);
  61. fclose($file);
  62.  }
  63.  else {
  64. error_message("Nie moge zapisac $dir/$filename.");
  65.  }
  66.  echo "<script>self.location.href='$PHP_SELF?dir=$dir&filename=$filename';</script>";
  67. }
  68.  
  69. function editor_page() {
  70.  global $dir, $filename, $default_filename;
  71.  $is_new = 0;
  72.  
  73.  if($filename == '') {
  74. $filename = $default_filename;
  75. $is_new = 1;
  76.  }
  77.  
  78.  if(!file_exists("$dir/$filename")) $is_new = 1;
  79.  if(!$is_new) {
  80.  edit_new_form();
  81.  ?>
  82.  <table border="1" width="100%">
  83.  <tr><td width="100%" colspan="2">
  84.  <center><strong>Statystyka <?php echo "$dir/$filename"; ?>
  85.  </td></tr>
  86.  
  87.  <?php
  88.  $file_info_array = file_info("$dir/$filename");
  89.  foreach($file_info_array as $key => $val) {
  90.  echo "<tr><td width=\"30%\">" . ucfirst($key) . "</td><td width=\"70%\">" . $val . "</td></tr>\n";
  91.  }
  92. ?>
  93. </table>
  94. <?php
  95. } else {
  96. echo "<center><strong>Edycja nowego pliku</strong></center>\n";
  97. }
  98. editor_form($dir, $filename, $is_new);
  99. }
  100.  
  101. if (empty($dir) || !ereg($dafault_dir, $dir)) {
  102. $dir = $default_dir;
  103. }
  104. switch ($action) {
  105. case "save_file";
  106. save_file();
  107. break;
  108. default:
  109. html_header();
  110. editor_page();
  111. html_footer();
  112. break;
  113. }
  114. ?>
  115.  
  116.  


W książce piszą że pliki trzeba na razie ładować tak: editor.php?filename=plik.txt . No i tak robię ale to nie działa. Ani nie chce tworzyć nowych plików. Na serwerze wszystkie uprawnienia są ok.
Przy włączenieu skryptu php daje tylko komentarze:
Kod
Warning: filesize(): Stat failed for /docs/new.txt (errno=2 - No such file or directory) in C:\Program Files\Apache Group\Apache2\htdocs\PROJEKT\common.php on line 49

Warning: filectime(): Stat failed for /docs/new.txt (errno=2 - No such file or directory) in C:\Program Files\Apache Group\Apache2\htdocs\PROJEKT\common.php on line 50

Warning: filemtime(): Stat failed for /docs/new.txt (errno=2 - No such file or directory) in C:\Program Files\Apache Group\Apache2\htdocs\PROJEKT\common.php on line 51

Warning: fileatime(): Stat failed for /docs/new.txt (errno=2 - No such file or directory) in C:\Program Files\Apache Group\Apache2\htdocs\PROJEKT\common.php on line 54

Warning: filegroup(): Stat failed for /docs/new.txt (errno=2 - No such file or directory) in C:\Program Files\Apache Group\Apache2\htdocs\PROJEKT\common.php on line 55

Warning: fileowner(): Stat failed for /docs/new.txt (errno=2 - No such file or directory) in C:\Program Files\Apache Group\Apache2\htdocs\PROJEKT\common.php on line 56

Ale nie ma żadnych błędów krytycznych.
Pomożecie ? sadsmiley02.gif

* "php 4 od podstaw"
wolguy
Jak na początku drugiego pliku dodasz to:
  1. <?php
  2.  
  3.  @$dir = $_GET['dir'];
  4.  @$filename = $_GET['file'];
  5.  
  6. ?>


i otworzysz tak: editor.php?dir=docs&file=new.txt to jest trochę lepiej biggrin.gif
RoxyFox
Co oznacza ta małpka ? Szukam w googlach ale nie da sie wpisać do googli małpy. Co to znaczy ?
Balon
te bledy oznaczaja ze new.txt nie istnieje... zrob ten plik i dopiero otworz to

edit: a ta malpka to znak tlumienia.. chociaz nie wiem po co w takim miejscu jak to nic nie zmieni...
RoxyFox
Ten plik istnieje. Ja już kombinowałem na wszystkie sposoby. Coś jest nietak ze skryptem.
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.