如何在PHP中使用unlink从服务器中删除图像路径?

时间:2015-07-30 04:17:34

标签: php

我差不多已经完成了我的项目,但我仍然遇到了一个小问题,我希望得到帮助。这是我的第一个PHP / mysqli项目,我仍然非常“绿色”。非常感谢任何帮助。

我已经能够成功上传和删除数据库中的图像,但是我似乎无法使用unlink命令从我的服务器中删除图像。

请在下面找到我在后台使用的代码(hotel-imgdelete.php):

    <?php
    include_once 'db_connect.php';
    include_once 'functions.php';

    sec_session_start();

// confirm that the 'id' variable has been set
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
    // get the 'id' variable from the URL
    $id = $_GET['id'];

    // delete image from server
        $path = "../hotels/";
        $image = "name";
        unlink($path.$image);

    // delete record from database
    if ($stmt = $mysqli->prepare("DELETE FROM hotels WHERE id = ? LIMIT 1"))
    {
        $stmt->bind_param("i",$id); 
        $stmt->execute();
        $stmt->close();
    }
    else
    {
        echo "ERROR: could not prepare SQL statement.";
    }
    $mysqli->close();

    // redirect user after delete is successful
    header("Location: ../home.php");
}
else
// if the 'id' variable isn't set, redirect the user
{
    header("Location: ../delete-hotel-images.php");
}

    ?>

这是我用来查看和选择要删除的图像的代码 (删除-酒店-images.php)

    <?php

      // get the records from the database




                    if ($result = $mysqli->query("SELECT * FROM hotels ORDER BY id"))

                    {
                            // display records if there are records to display
                            if ($result->num_rows > 0)
                            {                                        
                                    while ($row = $result->fetch_object())
                                    {
                                         $row->id;
                        echo        "<div id='partner'><img src='hotels/" . $row->name . "'></a><br><br>";
                        echo        "<center><a href='#' onclick='delete_user(". $row->id . ")'>Delete</a></center></div>";

                                    }
                            }
                            // if there are no records in the database, display an alert message
                            else
                            {
                                    echo "No results to display!";
                            }
                    }
                    // show an error if there is an issue with the database query
                    else
                    {
                            echo "Error: " . $mysqli->error;
                    }

                    // close database connection
                    $mysqli->close();

            ?>

2 个答案:

答案 0 :(得分:0)

我不完全确定你的文件系统是什么样的,或文件应该是什么,但看起来你正试图删除“../hotels/name”,因为$ image被设置为字符串“name”。

我认为这不是故意的,因此可能是那里的问题。但是,如果您尝试删除目录(因为它似乎没有文件扩展名),您将需要使用“rmdir”而不是“取消链接”。

图像如何在文件系统上布局?

答案 1 :(得分:0)

排序

        if (isset($_GET['id']) && is_numeric($_GET['id']))
{
    // get the 'id' variable from the URL
    $id = $_GET['id'];

            if  ($stmt = $mysqli->prepare("SELECT id, name FROM hotels WHERE id=?"));
                {
                $stmt->bind_param("i", $id);
                $stmt->execute();
                }
                $stmt->bind_result($id, $name);
                $stmt->fetch();
                $path = "../images/hotels/";
                $image = $name;
                unlink($path.$image);
                $stmt->close();

    include_once 'db_connect.php';
    include_once 'functions.php';



    // delete record from database
    if ($stmt = $mysqli->prepare("DELETE FROM hotels WHERE id = ? LIMIT 1"))
    {
        $stmt->bind_param("i",$id); 
        $stmt->execute();
        $stmt->close();
    }
    else
    {
        echo "ERROR: could not prepare SQL statement.";
    }
    $mysqli->close();

    // redirect user after delete is successful
    header("Location: ../home.php");
}
else
// if the 'id' variable isn't set, redirect the user
{
    header("Location: ../delete-hotel-images.php");
}