Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Unlink() function

Tags:

file

php

unlink

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.

like image 280
JLearner Avatar asked Jul 13 '12 03:07

JLearner


People also ask

How do you use the unlink function?

Unlink() function: The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure. The unlink() function in PHP accepts two-parameter.

How do I unlink a file?

Using the Unlink Command to Remove a File The unlink command is used to remove a single file and will not accept multiple arguments. It has no options other than --help and --version . The syntax is simple, invoke the command and pass a single filename as an argument to remove that file.

How do I unlink in Perl?

Syntax to declare unlink() function in Perl is as follows:unlink($filename); where List specifies a list with each item specifying the path to the location of the file that is to be deleted and filename specifies the name of the file that is to be deleted.

What is unlink C++?

This unlink() deletes the link named by pathname and decrements the link count for the file itself. pathname can refer to a pathname, a link, or a symbolic link. If the pathname refers to a symbolic link, unlink() removes the symbolic link but not any file or directory named by the contents of the symbolic link.


2 Answers

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';
like image 111
Marcio Mazzucato Avatar answered Oct 19 '22 21:10

Marcio Mazzucato


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.

like image 43
Travis Avatar answered Oct 19 '22 20:10

Travis