[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-04
7
Medium Priority
?
1,233 Views
Last Modified: 2014-03-01
...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?
0
Comment
Question by:jristov
6 Comments
 
LVL 5

Expert Comment

by:PragmatiCoder
ID: 36913912
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
0
 
LVL 10

Accepted Solution

by:
Derokorian earned 1000 total points
ID: 36914554
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

0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 36914964
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 36918163
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
0
 

Author Comment

by:jristov
ID: 39895787
I've requested that this question be deleted for the following reason:

abandoned long ago
0
 
LVL 111

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 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