How to use Unlink() function
Asked Answered
A

5

13

I'm trying to use PHP unlink() function to delete away the specific document in the folder. That particular folder has already been assigned to full rights to the IIS user.

Code:

$Path = './doc/stuffs/sample.docx';
if (unlink($Path)) {    
    echo "success";
} else {
    echo "fail";    
}

It keep return fail. The sample.docx does reside on that particular path. Kindly advise.

Atalanta answered 13/7, 2012 at 3:3 Comment(6)
Have you checked if you have permission to delete files? Addiotinally, try to use an absolute path, like this: $Path = '/doc/stuffs/sample.docx'Epifocal
yes. I can download that particular file so my path should be working.Atalanta
If you are certain the permissions are correct, I'm guessing the path is wrong, like Marcio suggests. Have you tried checking it with file_exists() ?Conakry
i did try var_dump(file_exists($Path)) but it gives me bool (false) :( but that particular path i set was used to download the document and it works...Atalanta
you might want to enable error reporting to check the error.Gauhati
Is your server running under Windows or Linux?Epifocal
E
13

I found this information in the comments of the function unlink()

Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change the file's owner. An example:

chown($tempDirectory . '/' . $fileName, 666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody" 
unlink($tempDirectory . '/' . $fileName); 

So try something like this:

$path = './doc/stuffs/sample.docx';

chown($path, 666);

if (unlink($path)) {
    echo 'success';
} else {
    echo 'fail';
}

EDIT 1

Try to use this in the path:

$path = '.'
         . DIRECTORY_SEPARATOR . 'doc'
         . DIRECTORY_SEPARATOR . 'stuffs'
         . DIRECTORY_SEPARATOR . 'sample.docx';
Epifocal answered 13/7, 2012 at 3:21 Comment(2)
it doesn't works. it falls into the else statement which is fail :(Atalanta
I did some careless mistakes on my file path by putting 1 '.' lees =.=Atalanta
C
9

Try this:

$Path = './doc/stuffs/sample.docx';
if (file_exists($Path)){
    if (unlink($Path)) {   
        echo "success";
    } else {
        echo "fail";    
    }   
} else {
    echo "file does not exist";
}

If you get file does not exist, you have the wrong path. If not, it may be a permissions issue.

Camise answered 13/7, 2012 at 3:7 Comment(4)
Hi travis. It give me file does not exist. But the path is also used to download the document and it works....Atalanta
./ means the path is relative to the path the script is run from. Is this script run from the same directory the script to download the file is run from?Camise
You mentioned the path is acquired from another page with a ./ in front of it. Can you provide more code or an in depth reference to where the file path is coming from?Camise
hi travis, the path can be parse to this form. I confirm because i can echo it out. It's the same as sample path given.Atalanta
P
2

This should work once you are done with the permission issue. Also try

ini_set('display_errors', 'On');  

That will tell you whats wrong

Playsuit answered 13/7, 2012 at 4:19 Comment(1)
try unlink($_SERVER['DOCUMENT_ROOT']."/doc/stuffs/sample.docx");Playsuit
V
1
define("BASE_URL", DIRECTORY_SEPARATOR . "book" . DIRECTORY_SEPARATOR);
define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'] . BASE_URL);

$path = "doc/stuffs/sample.docx";

if (unlink(ROOT_PATH . $Path)) {   
  echo "success";
} else {
  echo "fail";    
}

// http://localhost/book/doc/stuffs/sample.docx
// C:/xampp/htdocs\book\doc/stuffs/sample.docx
Valtin answered 30/11, 2017 at 11:47 Comment(0)
T
0

You need the full file path to the file of interest. For example: C:\doc\stuff\sample.docx. Try using __DIR__ or __FILE__ to get your relative file position so you can navigate to the file of interest.

Tablespoon answered 13/7, 2012 at 3:8 Comment(2)
I cant do that because that particular path was parse from another web page with './' in front.Atalanta
Are you trying to delete a file on another site's file system?Tablespoon

© 2022 - 2024 — McMap. All rights reserved.