无法查看/删除文件PHP


Cannot view/delete file PHP

此代码用于显示或删除在上一页的下拉框中选择的文件。有两个单选按钮,分别为"查看"或"删除"。视图的值为1。错误报告器只是显示没有可用的文件,即使有。有人能在我的代码中发现错误吗?任何地方都可能有语法吗?非常感谢。

<?php
  // error handling function
  function myErrorHandler($errno, $errstr) {
    echo "<b>Error: </b> [$errno] $errstr";
    echo "<br>";
    echo "End scripting";
    error_log("Error: [$errno] $errstr", 3, "error.log");
    die();
  }
  // set error handler
  set_error_handler("myErrorHandler");
  $option = $_POST['option'];
  $filename = $_POST['filename'];
  if (file_exists($filename)) {
     if ($option == "1") {
         $file = fopen($filename, "r");
         $line = "";
         while (!feof($file)) {
           $line .= fgets($file,1024). "<br>";
         }
             fclose($file);
       }
       else {
          unlink($filename)
            or die ("Cannot delete your file");
          $line = $filename. " deleted";
        }
      }
  else { trigger_error(date("h:i:sa")." -> "."no file exists...'n");}
?>
<html>
<head><title>RCM Site</title></head>
<div align = center>
<body background="bg.jpg">
<body>
  <div class = "rcmsite">RCM Site</div>
  <div class = "centre">
     <?php echo $line; ?>
  </div>
<p><a href = "congrats.html"> Go Back</a></p>
</body>
</html>

这是filunlink.php页面(上一页):

<?php
  $file_list ="";
  $path = "/var/www/rcm/";
  $show = array('.txt');
  $dir = opendir($path);
while (false != ($file = readdir($dir))) {
    $ext=substr($file,-4,4);
        if(in_array($ext, $show)){
            if (($file != ".") && ($file != ".."))  {
                $file_list .= "<option value = '"rcm/$file'">Go To $file</option>";
                }
    }
}
closedir($dir);
?>
<html><head><title>File viewer</title></head>
<div align = center>
<body background="bg.jpg">
<h3><i>File View</i></h3> 
<i>Select a file to view:</i> <br>
<form action = "fileunlink.php" method = "post">
Files in <?php echo($path); ?>
<select name = "filename">
  <?php echo ($file_list); ?>
</select>
<br><br>
<input type = "radio" name = "option" value = "1">View
<input type = "radio" name = "option" value = "2">Delete<br><br>
<input type = "submit" value = "Submit this form">
<p><a href = "congrats.html"> Go Back</a></p>
</form>
</body>
</html>

您可以尝试在第一个代码块中指定路径,使其读取:

<?php
  // error handling function
  function myErrorHandler($errno, $errstr) {
    echo "<b>Error: </b> [$errno] $errstr";
    echo "<br>";
    echo "End scripting";
    error_log("Error: [$errno] $errstr", 3, "error.log");
    die();
  }
  // set error handler
  set_error_handler("myErrorHandler");
  $option = $_POST['option'];
  $filename = $_POST['filename'];
  $path = "/var/www/rcm/";
  if (file_exists($path.$filename)) {
     if ($option == "1") {
         $file = fopen($path.$filename, "r");
         $line = "";
         while (!feof($file)) {
           $line .= fgets($file,1024). "<br>";
         }
             fclose($file);
       }
       else {
          unlink($path.$filename)
            or die ("Cannot delete your file");
          $line = $filename. " deleted";
        }
      }
  else { trigger_error(date("h:i:sa")." -> "."no file exists...'n");}
?>
<html>
<head><title>RCM Site</title></head>
<div align = center>
<body background="bg.jpg">
<body>
  <div class = "rcmsite">RCM Site</div>
  <div class = "centre">
     <?php echo $line; ?>
  </div>
<p><a href = "congrats.html"> Go Back</a></p>
</body>
</html>