I am generally a Pedant in and of myself and I don’t like it when there is rubbish left in a particular directory or database by means of program crashes or errors. I have never written serious projects and in order to get a lesson by reading your comments, I will ask a series of questions in detail writing everything down.
For example, there is a script that allows two users to communicate with each other, share photos, videos, music, etc., and the principle of the script is as follows:
The user prints a message, uploads a photo (files are loaded into a temporary folder by ajax) and a music file, then clicks the "Send" button and the data flies to the server, and the following happens on the server:
A text message is written to the database, then the next step is to write to another photo info table (path to the photo, dimensions, etc.), and then move the photo from the temporary directory to a permanent one, the same with the music file.
Everything seemed to be fine, we got the expected result, but what if after the text message is recorded in the database and the file information is also recorded, then after that there will be a failure? After all, failures can be? This is a technique. With server, internet, etc. anything can happen during execution.
At the exit, we receive a message that, without attachments and moreover, half of the information about attachments was recorded, and half was not and it is still not clear where the files themselves remained.
The idea came to my mind at the beginning of the execution of a code to be implemented something like a map, for example, from an array and write all the information into it, namely:
- Full path to the files from the temporary folder
- The full path to the files where we will have to move the files from the temporarily folder
- And other possible information information
And this card immediately first thing to write to the table (I will not describe where, the main thing is to understand that this infa recorded)
And at the end of the script, if everything went well, then we delete the map, and if the map, for example, after updating the page, remains in the table, then the script worked incorrectly and just run on this map and delete everything from everywhere.
I’m not trying to invent or discover something, I’d just like to know if this should be done? Or is there a simpler way I've never heard of? Or maybe all this wild nonsense?
I will be happy to hear from you and get good advice.
Thank!!!