Welcome Guest, Not a member yet? Register   Sign In
Chicken or the Egg ?
#1

[eluser]charlie spider[/eluser]
When writing code to delete a file from the server, you think it is best to:

1) Delete the file's database entry first, then unlink the file

OR

2) Unlink the file first, then delete the file's database entry.
#2

[eluser]Rey Philip Regis[/eluser]
well...the chicken comes first before the egg..Cause there will be no egg if there is no chicken....Ohhhh....The egg comes first before the chicken..Cause if there's no egg there would be no chicken. Waaaaa! This question has been debate many times already of who or what comes first.....And I think no one has proven comes first..

Anyways, about your question for me you could delete the file first then the data in the database or you can do the opposite. I think there will be no problem with that as long as you know what you are deleting. Its a matter of choosing what you like..

Good day..
#3

[eluser]Teks[/eluser]
Your algorithm for retrieving an existing file should be something like this:

1) retrieve the entry from the database (including file location on the server)
2) check that file actually exists on the server:
--a) file exists on the server: move on to step 3
--b) file does not exist on the server - warn the user, and perhaps give option to:
----i) upload a replacement file (exit and go to 'upload file' function)
----ii) delete record from database (and exit)
3) retrieve and display/download/present the file to the user

So, if you think about it, it would make sense for your delete algorithm to be something like this:

1) check that file exists on server:
-- a) if file does not exist on server (record in database is bogus) - skip to step 2
-- b) if file exists:
----i) try to delete file from server:
------A) success - move to step 2
------B) fail to delete - abort everything
2) delete record from database.

Even if you experience a problem with the final operation (=deleting the file record from the database), this will be picked up by your 'retrieving' algorithm next time a user tries to access the file.

Short answer: delete file first, record second.




Theme © iAndrew 2016 - Forum software by © MyBB