简体   繁体   中英

How to delete the image path from a server using unlink in PHP?

I've almost finished my project but I'm stuck on a small problem I'm hoping to get help with. This is my first PHP/mysqli project and I'm still very "green". Any help is much appreciated.

I have been able to successfully upload and delete images from my database, however I can't seem to get the unlink command to delete the images from my server.

Please find below the code I am using in the background (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");
}

    ?>

This is the code I am using to view and select the images to delete (delete-hotel-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();

            ?>

I'm not entirely sure what your filesystem looks like, or what the file is supposed to be, but it looks like you're trying to delete "../hotels/name", since $image is set to the string "name".

I'm assuming this wasn't intentional so that could be the problem there. If, however, you are trying to delete a directory (since it appears to have no file extension) you will need to use "rmdir" and not "unlink".

How are the images laid out on your filesystem?

sorted

        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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM