PHP unlink() -- if the file delete is unsuccessful.......

Posted on 2011-10-04
Last Modified: 2014-03-01 there a way around it, perhaps by the PHP version of "error handling", etc.?

I know that unlink will simply return a value of false (0) if the delete is unsuccessful, however (at least in my case) it will also generate a PHP error message of "permission denied", evidently because the user still has that document (a PDF generated by the pdftk.exe executable) open in the browser.

I need some way, if possible, to know in advance if the unlink is going to be unsuccesful, or otherwise a way to simply skip the unlink operation, without an error message, if it will be unsuccessful. Any ideas?
Question by:jristov
    LVL 5

    Expert Comment

    It doesn't matter if the user has the document opened in his browser, cause it's already loaded in memory, so that's not the problem.

    Try to do a chmod($path_to_file, 777) before deleting the file, that way you ensure that php will be able to delete the file
    LVL 10

    Accepted Solution

    The @operator before a function suppresses all errors thrown by that function. However you could use the chmod method suggested by Pragmati so that it just works.

    $file = 'some_file.pdf';
    if( file_exists($file) ) {
       // unlink failed
    } else {
       // unlink successful

    Open in new window

    LVL 15

    Expert Comment

    just an addon to both EE comments. Check the owner of the file. some times we provide the client to dump or use the file from different owner and try to delete it with different owner.
    use 'chown' for it .
    ya @ will suppress the errors but try to avoid its use. even after doing the above procedure if it fails.
    Than try for it.

    Check the path of files which has to be deleted
    LVL 107

    Assisted Solution

    by:Ray Paseur
    If you're sure you want to unlink() the file, use @unlink() and suppress the message.  Then test the return value from unlink().  If it is false, store the file name somewhere for future processing or manual correction.

    Just a general note about using @ notation.  Avoid it unless you are absolutely sure you do not care about the messages and you are absolutely sure you understand the consequences of the code's behavior without seeing the messages.  I can't tell you how many times I have seen someone confounded by writing something like this, then not understanding why there is no output:


    Best regards, ~Ray

    Author Comment

    I've requested that this question be deleted for the following reason:

    abandoned long ago
    LVL 107

    Expert Comment

    by:Ray Paseur
    I believe there were perfectly good answers given here and here.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now