Fixing Corrupted or Broken Files and Databases in WordPress
Have you found that your WordPress site’s on the fritz and all you can say is “I didn’t do anything, I swear!?” Yet, you see errors that say your files or database are broken, missing or corrupted.
Anything from posts and categories sporadically disappearing and reappearing to error messages like:
“Warning: require_once(path/to/file.php) [function.require-once]: failed to open stream: No such file or directory in…”
“Cannot establish database connection.”
Not to worry because today, I’ll explain how these kinds of broken, missing and corrupted errors happen, how to fix them and most importantly, I’ll share why it’s more than likely not your fault.
Hey, What’s the Big Idea Here?
It can be difficult to track down the exact cause of any of these errors because there’s an unfortunate number of probable causes.
Here are the top reasons why your database could be corrupted, missing or otherwise broken:
- Your database credentials aren’t correctly entered in your wp-config.php file
- Your server crashed
- There was a server hardware failure
- A bug such as in MySQL, the version of PHP you’re using or your plugins and themes
- There was a server interruption such as your PHP memory limit being reached
- You could have been hacked
There’s also an even larger list of top reasons why your files could be in the same situation:
- Your file permissions aren’t set up correctly
- Your site or server’s firewall is blocking certain parts of your site
- You entered the wrong site URL under Settings > General
- Third party plugins auto deleted content or broke file paths
- You installed an SSL certificate and forgot to replace your image links
- Permalinks were improperly set up
- Your .htaccess file has errors in it
- Images were uploaded in the wrong directory
- There was an interruption while uploading content
- There’s a bug in a plugin, script or theme you’re using
- A server-level issue is afoot
- You could have been hacked
No matter what the reason, there are a few ways you can fix your files or database.
Backup and Restore
The absolute easiest way to fix and broken file or database issue is to restore your site from a working backup of your site. If you don’t have a backup that you can restore, then there are other fixes that you can try.
Before you do give the other options a shot, you should backup your entire site. Even if it’s broken, things could still get worse so it’s better to have something rather than nothing at all.
Here’s a list of posts you can check out for details on backing up your site:
- Backup Plugins Aren’t About Backing up, They’re About Restoring
- How to Backup Your WordPress Website (and Multisite) Using Snapshot
- 4 Top WordPress Multisite Backup Solutions Tested and Reviewed
- 7 Top Premium and Freemium WordPress Backup Plugins Reviewed
- 11 Best Free Quality Backup Plugins for Protecting Your WordPress Site
- Creating a Manual Backup of WordPress When It’s Down or Locked
- Testing WordPress Website Backups to Avoid Future Disasters
- Introducing WPMU DEV Cloud Backups (Members Get 10GB for Free!)
- How to Back Up Your WordPress Backups for Bulletproof Protection
Once your site is all backed up and waiting on the sidelines in case disaster strikes, you’re ready to fix your site.
Repairing a Broken Database
There are a couple ways you can repair a broken database.
If you’re seeing a “Cannot establish database connection error,” you can check out this post for details on how to fix it: How to Fix “Error Establishing Database Connection” for WordPress.
There’s also one more thing you can try that works for most issues including the event that your database is corrupted.
You can add this line to your wp-config.php file, which is located in the root of your install:
You can add it anywhere in your file as long as it isn’t smack dab in the middle of a function, for example. I recommend placing it right before the following line:
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox - free!
For details on how to edit your wp-config.php file, check out the How to Use FTP Properly with WordPress post for more details.
Once you have added the line mentioned above, you can visit the database repair page: http://your-site.com/wp-admin/maint/repair.php and if you have an SSL certificate installed, you would visit https://your-site.com/wp-admin/maint/repair.php.
Don’t forget to replace your-site.com with your actual domain.
Keep in mind that you don’t need to be logged in to view this page so when you’re done repairing your database, be sure to remove the line mentioned earlier. Otherwise, anyone could get to that page.
When you visit the link, you can choose to click the Repair Database button or you can optimize your database whilst you repair it by clicking the Repair and Optimize Database button.
Once the process is complete, your site should be all fixed up and ready to go.
If that doesn’t work, you can repair your database through phpMyAdmin.
After logging in, click on your database on the list to the left, then when your database tables load on the page, scroll down and click the Check All box. In the drop down box next to the check box, select Repair table.
Your database tables are automatically repaired and your site should be functional again.
Repairing Broken, Missing or Corrupted Files
But what if it’s your files that are missing, corrupted or broken? You can fix them, too.
If you recently installed an SSL certificate to your site, your images could appear missing. To fix this, replace your image links to include the https prefix. For details, check out the post Replacing Image Links in WordPress After Installing an SSL Certificate.
For most other issues, replacing the problem file with a fresh copy should resolve the issue.
After uncompressing the ZIP file, you can transfer the new files to your site. Just be sure to leave your /wp-content/ directory untouched or you stand to loose all the media files and images you uploaded to your site.
You could also try this with the plugins and themes you have installed. The only difference is that instead of downloading a fresh copy of WordPress, you would download a new copy of your plugin and theme files.
If All Else Fails…
If you have tried these fixes, but you haven’t had any luck getting your site back up, you can try turning on the WordPress debugging mode. For details on how to do this, check out Debugging WordPress: How to Use WP_DEBUG.
You can also try checking your error logs to see if you can identify the issue and also send them to your host to look over and analyze. Look for a file called debug.log under your /wp-content/ folder.
If that doesn’t resolve the issue, it’s time to contact your host. It’s possible that there could be a server issue such as a misconfiguration or it could have malfunctioned. If this is the case, contacting your host and having them fix it is the only way you can resolve your issue.
Well, there is one other way, but it’s not for the faint of heart:
If you don’t have a backup and you’re okay with losing your site completely, you can start over and delete your site, then reinstall WordPress.
By now, your site should be running as good as new and you shouldn’t see anymore errors that point to a missing, corrupted or otherwise broken database or files.
To avoid the long process of fixing your site if this ever happens again, it’s recommended that you regularly backup your site.
Don’t forget that a WPMU DEV membership gets you our Snapshot Pro plugin to backup your site as well as 10 GB of managed cloud backups for free, plus everything else we have… everything. If you don’t already have a membership, you can sign up for free and try it on for size.