Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 865567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:39:32+00:00 2026-05-15T09:39:32+00:00

I have a MySQL table which basically serves as a file index. The primary

  • 0

I have a MySQL table which basically serves as a file index. The primary key of each record is also the name of a file in a directory on my web host.

When the user wants to delete a file from the system, I want to ensure some kind of transaction safety, i.e. if something goes wrong while deleting the file the record is not erased, and if for some reason the database server dies the file won’t be erased. Either event occurring would be very unlikely, but if there’s even the slightest chance of a problem I want to prevent it.

Unfortunately I have absolutely no idea how to implement this. Would I need to work out which is less likely to fail, and simply assume that it never will? Are there any known best practices for this?

Oh and here’s the kicker – my web host only supports MyISAM tables, so no MySQL transactions for me.

In case it matters, I’m using PHP as my server-side scripting language.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:39:32+00:00Added an answer on May 15, 2026 at 9:39 am

    Whether the file is “Deleted” from the DB via a UPDATE or a DELETE of a row, the problem is the same — the database + file operations are not atomic. Neither an UPDATE or a DELETE are safer than the other, they’re both transactions in a database whereas the file operation is not.

    The solution is that there is never any conflict as to the state of the data. Only one source is considered “the truth” and the other reflects that truth. That way if there’s ever an inconsistency between the two, you know what the “truth” is. In fact, there is never a “logical” inconsistency, only the aftermath manifested by physical artifacts on the disk.

    In most cases, the Database is a better representation of The Truth.

    Here’s the truth table:

    File Exists -- DB Record exists -- Truth
        Yes             No             File does not exist
        Yes             Yes            File does exist
        No              Yes            File does exist, but its in error.
        No              No             File does not exist
    

    Operationally, here’s how this works.

    To create a file, copy the file to the final destination, then make an entry in the DB.

    If the file copy fails, you don’t update the DB.
    If the file copy succeeds, but the DB is not updated, the file “does not exist”, so back to step one.
    If the file copy succeeds and the DB update succeeds, then everything is A-OK

    To delete a file, first update the DB to show the file is deleted.
    If the DB update succeeds, then delete the actual file.
    If the DB update does not, then do not delete the file.
    If the file delete fails, no problem — the file is still “deleted” because the DB says so.

    If you follow the work flow, there’s “no way” that the file should be missing while the DB says it exists. If the file goes missing, you have an undefined state, that you will need to resolve. But this shouldn’t happen barring someone walking on your file system.

    The DB transactions help keep things honest.

    Occasionally, as Jonathan mentioned, you should run some kind of scavenging, syncing process to make sure there aren’t any rogue files. but even then, that’s really not an issue save for file space, especially if the file names of the actual files have nothing to do with the original file names. (i.e. they’re synthetic files names) That way you don’t have to worry about overwrites etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.