How to unlink image in php

How to unlink image in php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Deleting Records Often Leaves Images Behind

When you delete a database record, it doesn't automatically erase the physical file on your server's filesystem. The scenario described involves fetching an image path stored in a column and attempting to remove that file using @unlink(). While the code successfully deletes the row from the database, the image remains visible because the unlink operation failed silently or was never executed against the correct logic flow.

The root cause lies in how the $image['image'] variable is being used versus what unlink() actually requires. In your example, you are echoing the full URL:
http://www.example.com/folder/images/image_name.jpeg

Passing this absolute URL directly to @unlink() will fail on almost every Linux/Unix server environment. The operating system's file manager functions expect a relative path or an absolute filesystem path starting with /, not a web-accessible URL containing the domain name. When you pass http://... to unlink(), it treats the string as a filename literally, looks for a directory named http in your current working directory, and naturally cannot find the file at that location. Consequently, the function returns false, often suppressed by the @ error control operator, leaving the image untouched.

To resolve this, you must convert the stored URL into a valid filesystem path before calling unlink(). If your database stores http://www.example.com/folder/images/image_name.jpeg, and your web root is /var/www/html, you need to strip the domain and protocol prefix to get /folder/images/image_name.jpeg.

Here is a more robust approach using modern PHP practices:

$id = $_GET['id']; // In production, sanitize this input or use prepared statements with PDO
$baseUrl = 'http://www.example.com'; 
$dbImagePath = $image['image']; // e.g., "http://www.example.com/folder/images/image_name.jpeg"

// Convert URL to filesystem path by replacing the domain with the server root
$serverRoot = '/var/www/html'; // Adjust this based on your actual web server configuration
$fileSystemPath = str_replace($baseUrl, $serverRoot, $dbImagePath);

// Now unlink works correctly because we are passing a valid filesystem path
if (@unlink($fileSystemPath)) {
    // File deleted successfully
} else {
    // Handle the error (e.g., file not found or permission denied)
}

$result = mysql_query("delete from table_name where question_id='$id'");

It is critical to understand that mixing database logic with filesystem operations requires precise path management. The @ symbol suppresses errors, which is dangerous in production code. If the image is missing for some reason (perhaps it was moved or deleted via a different process), unlink() will fail silently, and you might miss a critical error condition during debugging. You should remove the @ sign to see actual error messages via error_log or php_error_log.

Furthermore, relying on the deprecated mysql_* extension is a significant security risk. These functions lack support for prepared statements, making your application highly susceptible to SQL injection attacks when dealing with user input like $id. Migrating to PDO or MySQLi allows you to write queries that safely handle special characters in IDs and paths without manually escaping every variable.

Finally, consider the permissions of the web server user (usually www-data or apache). Even if your path conversion is correct, if the web server lacks read/write/execute permissions on the specific folder, unlink() will throw a permission error. Ensuring that the directory structure matches the expectations of the file system is just as important as writing the PHP code itself.

Our network