PHP取消链接文件错误是一个目录

时间:2015-08-04 17:20:22

标签: php unlink

我正忙着研究一本关于php的书,他们正在练习从数据库中删除记录。我遇到的问题是删除与数据库条目关联的图像。我有一个定义的常量:

define(GW_UPLOADPATH, 'images/')

在名为appvars.php的文件中。这是remove.php

<?php
    require_once 'authorize.php';
?>
<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>        
        <?php
        require_once 'appvars.php';
        require_once 'connectionvars.php';

        if(isset($_GET['id']) && isset($_GET['name']) && isset($_GET['score']) && isset($_GET['date'])
                && isset($_GET['screenshot'])){
            $id = $_GET['id'];
            $name = $_GET['name'];
            $score = $_GET['score'];
            $date = $_GET['date'];
            $screenshot = $_GET['screenshot'];
        } else if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['score']) && isset($_POST['date'])){
            $id = $_POST['id'];
            $name = $_POST['name'];
            $score = $_POST['score'];
            $date = $_POST['date'];

        } else {
            echo 'No record selected';
        }

            if(isset($_POST['submit'])){
                if(($_POST['confirm'] == 'Yes') && is_file(GW_UPLOADPATH.$screenshot)){

                    unlink(trim(GW_UPLOADPATH.$screenshot));

                    $query = "DELETE from guitarwars where id = $id limit 1";
                    mysqli_query($dbc, $query);
                    mysqli_close($dbc);

                    echo '<p class="error">The score of ' . $score . ' for' . $name . ' was successfully deleted</p>';
                } else {
                    echo '<p class="error">Error removing record</p>';
                }
            }

            else if(isset ($id) && isset($name) && isset($date) && isset($score) && isset($screenshot)){
                echo '<p>Are you sure you want to delete the following high score?</p>';
                echo '<p>Name: ' . $name . '<br />Date: ' . $date . '<br />Score: ' . $score . '<br />'
                        . 'PATH:' . GW_UPLOADPATH.$screenshot. '</p>' ;
                echo '<form method="POST" action="remove.php">';
                echo '<input type="radio" name="confirm" value="Yes" />Yes<br />';
                echo '<input type="radio" name="confirm" value="No" checked="checked" />No<br />';
                echo '<input type="submit" name="submit" value="Submit">';
                echo '<input type="hidden" name="id" value="' . $id . '">';
                echo '<input type="hidden" name="name" value="' . $name . '">';
                echo '<input type="hidden" name="date" value="' . $date . '">';
                echo '<input type="hidden" name="score" value="' . $score . '">';
                echo '</form>';

            }

            echo '<p><a href="admin.php">Back to Admin page</a></p>';

        ?>
    </body>
</html>

数据库删除条目100%,但我得到一个错误,即图像是目录。如果您查看html,它会将路径报告为images / imageName.gif

我添加了is_file()以试图弄清楚发生了什么,结果我现在得到了我分配的错误消息“Error removal record”。所以我认为,它没有看到我的imageName.gif作为文件。不确定如何删除该文件,该书和平使用取消链接。

非常感谢任何指导

ADDED:addscore.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Guitar Wars - Add Your High Score</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Guitar Wars - Add Your High Score</h2>

<?php
require_once 'appvars.php';
require_once 'connectionvars.php';

  if (isset($_POST['submit'])) {
    // Grab the score data from the POST
    $name = $_POST['name'];
    $score = $_POST['score'];
    $screenshot = $_FILES['screenshot']['name'];
    $screenshot_type = $_FILES['screenshot']['type'];
    $screenshot_size = $_FILES['screenshot']['size'];


    if (!empty($name) && !empty($score) && !empty($screenshot)) {
        if((($screenshot_type == 'image/gif') || ($screenshot_type == 'image/jpeg') || ($screenshot_type == 'image/pjpeg')
                || ($screenshot_type == 'image/png')) && (($screenshot_size > 0) && ($screenshot_size <= GW_MAXUPLOADSIZE))){
        $target = GW_UPLOADPATH.$screenshot;
        if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)){


      // Write the data to the database
      $query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', '$screenshot')";
      mysqli_query($dbc, $query) or die('Error inserting data: ' . mysqli_error($dbc));

      // Confirm success with the user
      echo '<p>Thanks for adding your new high score!</p>';
      echo '<p><strong>Name:</strong> ' . $name . '<br />';
      echo '<strong>Score:</strong> ' . $score . '<br />';
      echo '<img src="' . GW_UPLOADPATH.$screenshot . '" alt="screenshot image" /></p>';
      echo '<p><a href="index.php">&lt;&lt; Back to high scores</a></p>';

      // Clear the score data to clear the form
      $name = "";
      $score = "";

      mysqli_close($dbc);
        }
    } else {
        echo '<p class="error">Please ensure image file is corrent format and less than ' . (GW_MAXUPLOADSIZE / 1024) .
                'Kb</p>';
    }

    @unlink($_FILES['screenshot']['tmp_name']);

    }


    else {
      echo '<p class="error">Please enter all of the information to add your high score.</p>';
    }


  }

?>

  <hr />
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <input type="hidden" name="MAX_FILE_SIZE" value="32768"/>
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />
    <label for="score">Score:</label>
    <input type="text" id="score" name="score" value="<?php if (!empty($score)) echo $score; ?>" /><br />
    <label for="screenshot">Screen Shot:</label>
    <input type="file" id="screenshot" name="screenshot" />
    <hr />
    <input type="submit" value="Add" name="submit" />
  </form>
</body> 
</html>

1 个答案:

答案 0 :(得分:1)

我认为问题是$screenshot未定义......

稍微简化代码,你有:

if(isset($_GET['screenshot'])) {
    $screenshot = $_GET['screenshot'];
} else if(isset($POST['id')) {

}

if(isset($_POST['submit'])){
    if(($_POST['confirm'] == 'Yes') &&  is_file(GW_UPLOADPATH.$screenshot)){

所以......假设您没有做一些非常奇怪的事情,请求将是GET请求或POST请求。如果它是GET请求,您只需设置$screenshot,但仅在POST请求中检查is_file。所以你正在检查is_file("images/")并且(正确地)告诉你它是一个目录。

试试这个:

else if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['score']) && isset($_POST['date']) && isset($_POST['screenshot'])){
    $id = $_POST['id'];
    $name = $_POST['name'];
    $score = $_POST['score'];
    $date = $_POST['date'];
    $screenshot = $_POST['screenshot']; //<-- add this line
}

...

echo '<form method="POST" action="remove.php">';
echo '<input type="radio" name="confirm" value="Yes" />Yes<br />';
echo '<input type="radio" name="confirm" value="No" checked="checked" />No<br />';
echo '<input type="hidden" name="screenshot" value="$screenshot" />'; //<-- add this line