jristov
asked on
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.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)?
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.
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.