Pdftk.exe -- permissions issues when deleting generated PDFs

I am using the common PDF command line program, pdftk.exe, to generated custom-made PDFs with variable data. In time, there is a need to delete some of these, for which I am using the PHP unlink() function.

Every once in a while, one rogue PDF that has been generated from pdftk.exe (called from the same PHP program) coughs up some permissions issues, keeping it from being deleted. I am perplexed because it is only this one rogue one once in a while, and all the others delete just fine. I also don't think it is an issue with the specific BASE pdf used to generate the one with variable data, because I know I have successfully deleted some generated from that same base PDF.

Can you clue me in? What could be causing this just once in a while? There are very few clues, and like I said the base PDF doesn't appear to be a key to the cause.
jristovAsked:
Who is Participating?
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.

Ray PaseurCommented:
Permissions issues are usually driven by the interplay of a program, a file system and an operating system.  If you want to tell us a little more about those things we might be able to offer some help.  The situation you describe might be driven by a chmod() statement in a conditional control structure.
0
Karl Heinz KremerCommented:
Is it possible that the file that you want to remove is actually in use (e.g. open in Reader or any other application, or being served by the web server)?
0

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
jristovAuthor Commented:
khkremer: Yes, it appears you have hit the nail right on the head! Thank you! But, a related question that pops up is: How do I know if this file is currently open or being used somewhere? Is there a way in PHP to know this (perhaps through an "error handler" that returns the permission denied error)?
0
Karl Heinz KremerCommented:
It looks like you are using Windows (based on the .exe extension to pdftk), this is not a platform I am very familiar with.

The unlink() function returns a boolean: TRUE on success and FALSE on failure. You should evaluate that return value to see if your unlink call was successful. I would hope that it reports a problem if the call fails due to the busy condition. Usually (again, this may not be valid for Windows), you can move a file that is open, so you could just move it out of the way, and then when in certain intervals process the "these files were in use at one point in time, but should be deleted" directory and try to delete the files in it.

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.