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

...is 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?
jristovAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PragmatiCoderCommented:
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
DerokorianCommented:
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.

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InsoftserviceCommented:
hi,
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
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ray PaseurCommented:
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:

@mysql_connect()

Best regards, ~Ray
jristovAuthor Commented:
I've requested that this question be deleted for the following reason:

abandoned long ago
Ray PaseurCommented:
I believe there were perfectly good answers given here and here.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.