Hi all, here i got a problem where i want to delete file uploaded..my script
just remove from database but not remove from directory.. how can i delete from database and directory at a same time?? Please help me..

Dani AI

Generated

Short summary and practical checklist that complements the replies by and :

Common reasons unlink() appears to “not work” are wrong path, directory vs file permission, open_basedir/SELinux restrictions, or the file being locked by another process. was right to suggest checking existence first; ’s idea to test unlink in the same directory is a simple, useful diagnostic. Avoid the error-suppression operator (@) — check return values and log failures so you know why deletion failed.

Safer, more robust pattern (high level)

  • Do not trust a raw filename from GET. Pass an ID, fetch the stored filename/path from the database, then validate it.
  • Strip any path segments (use basename) or otherwise enforce that the resolved path sits inside your uploads directory.
  • Use prepared statements (PDO or mysqli) instead of deprecated mysql_* functions to avoid SQL injection.
  • Attempt to delete the file first, check the result, and only then remove or update the DB row. If deleting the file fails, log the error and either retry later (cron) or mark the DB record so an admin can investigate.

Example safe flow (illustrative)

// fetch safe filename by ID (PDO), then:
$filename = basename($row['filename']); 
$uploadDir = __DIR__ . '/uploads';
$full = $uploadDir . DIRECTORY_SEPARATOR . $filename;

if (is_file($full) && unlink($full)) {
    // prepared DELETE statement to remove DB record
} else {
    // log and keep DB record (or mark for cleanup)
}

Quick troubleshooting checklist

  • Echo the full path you’re trying to delete to confirm it’s correct.
  • Ensure the web server user owns (or has write permission on) the upload directory — on POSIX you need directory write permission to remove an entry.
  • Check open_basedir, SELinux, or Windows file locks.
  • Add logging around unlink() and DB ops so you can see which step failed.

Recommended Answers

All 5 Replies

this my delete code

<?php 
include 'connection/db_connect.php';
$attcid = $_GET['attcid'];
$username = $_GET['username'];

$sql="DELETE FROM attachment WHERE attcid = '$attcid'";
mysql_query($sql);

?>

try.. make sure the directory and the file name are properly assigned below.

if (file_exists($attcid."whatever.extensionOfFile"){
						@unlink($attcid."whatever.extensionOfFile") ;
					}
else{

echo "Are you kidding me? This file don't even exist. I already looked.";

}
commented: Useful post +6

thanks veedeoo for suggestion..still not work.. hurm.. :(

unlink works, if it doesn't then there's problem with your path, if you doubt it's functionality, try doing simple test, like unlink(test.php); putting unlink script and test.php into the same directory

thanks all!! it works!! when second try i change by calling the file name.. hurmm
thank you so much!! This my latest code.. :)

<?php
$fileName=$_GET['fileName'];
$sql="DELETE FROM attachment WHERE fileName = '$fileName'";
unlink("path".$fileName);
mysql_query($sql);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.