【问题标题】:PHP unlink file error IS A DIRECTORYPHP取消链接文件错误是一个目录
【发布时间】:2015-08-04 17:20:22
【问题描述】:

我正忙着研究一本关于 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() 来尝试弄清楚发生了什么,因此我现在得到了分配的错误消息“删除记录时出错”。所以我认为,它没有将我的 imageName.gif 视为文件。不知道如何删除文件,这本书和平地使用取消链接。

非常感谢任何指导

添加: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>

【问题讨论】:

  • 我们能看到你创建文件的代码吗?另外,不相关,但你可以做if(isset($id, $name, $date, $score, $screenshot)) {
  • 嘿戴夫,确定我已经用addscore.php编辑了我的帖子,感谢您提供的额外信息,我会记住这一点,这本书没有提到

标签: php unlink


【解决方案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

【讨论】:

  • 嘿戴夫,我注意到书中一些示例的未定义​​警告,但认为这可能是版本问题。我不确定我是否完全跟随你。 $screenshot 来自 GET 方法。 删除 ';
  • 做到了!太棒了,感谢您帮助我解决这个问题。我对您发布的内容稍作更改 echo '';而不是 value="$screenshot" 你的方式做同样的事情吗?我还在学习各种语法
  • 谢谢!我正在使用相同的教科书,这帮助我解决了同样的问题。
猜你喜欢
  • 2023-03-08
  • 2015-10-27
  • 2020-04-09
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多