Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 2138

PHP unlink() not working to delete files?

PHP unlink() not working to delete files

my code is deleting in database properly but cant deleted from file how pls help me how to delete data in my uploaded file?

<?php
$id = $_POST['id'];
include_once('db.php');
$objDbCon = new db_connect();
$strSQL   = "DELETE FROM study_material WHERE id='$id'";
$objQuery = $objDbCon->Query($strSQL);
unlink('../uploaded/');
if ($objQuery) {
    echo "Delete Sucessfully";
} else {enter code here
    echo "Error";
}
?>

Upvotes: 3

Views: 3752

Answers (5)

Peter B
Peter B

Reputation: 467

It's important to make sure that the files/folders have the correct permissions also in addition to the other answers.

PHP usually runs as the user and so if you don't have the correct permissions to manage the file, you won't be able to do anything with it.

In SSH:

chown -R user:user /path/to/folder

This will set all of your files to the user PHP is using. Obviously replace user:user with root:root for example.

Upvotes: 0

Nb12se
Nb12se

Reputation: 513

Use this,

unlink($_SERVER['DOCUMENT_ROOT']).'/projectname'.$filename);

where

$filename is ./folderuploads/filename.extension

Upvotes: 2

Indrajeet Singh
Indrajeet Singh

Reputation: 2989

Try this code:
1:- select file from your table.
2:- unlink file from directory
3:- last delete row from table
unlink('../uploaded/filename');

Upvotes: 0

Techroshni
Techroshni

Reputation: 541

The parameter for unlink() function must be a filename rather than a directory.

Upvotes: 0

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

unlink() is used to delete files and you try to delete directory using this function ('../uploaded/' is directory not a file). If you want to remove empty directory you need to use rmdir() function instead

Upvotes: 1

Related Questions