Tero Karvinen - Articles - Downloads - Courses - Contact

Table of Contents

Empty File Handling for Wordpress HTML Importer Plugin

HTML importer plugin (by Stephanie Leary) can convert static HTML pages to WordPress posts. However, some of the plugins error mesages could be improved.

When HTML importer 1.21 meets and empty file, it dies with a cryptic error message: “The PHP functions fopen and file_get_contents have both failed. We can’t import any files without these functions. Please ask your server administrator if they are enabled.”

But these functions are fine! The problem is the empty file. It might also fail with files that don’t have the defined content tag.

HTML importer displays a list of processed files, but this doesn’t show the name of problem file - it wasn’t processed.

I modified the error message to include the name of the offending file.

Before:

The PHP functions fopen and file_get_contents have both failed. 
We can't import any files without these functions. Please ask your server administrator if they are enabled.

After:

Got empty contents while processing "/home/tero/html/empty.html". [...]

Patch

=== modified file 'wp-content/plugins/import-html-pages/html-import.php'
--- wp-content/plugins/import-html-pages/html-import.php        2010-11-24 18:06:57 +0000
+++ wp-content/plugins/import-html-pages/html-import.php        2010-11-24 18:13:30 +0000
@@ -564,7 +564,7 @@
                        // process the HTML file
                        $contents = @fopen($path);  // read entire file
                        if (empty($contents)) $contents = @file_get_contents($path);  // read entire file
-                       if (empty($contents)) wp_die("The PHP functions fopen and file_get_contents have both failed. We can't import any files without these functions. Please ask your server administrator if they are enabled.");
+                       if (empty($contents)) wp_die("Got empty contents while processing \"$path\". The PHP functions fopen and file_get_contents have both failed. We can't import any files without these functions. Please ask your server administrator if they are enabled.");
                        if (function_exists('mb_convert_encoding')) $encoded = mb_convert_encoding($contents, 'HTML-ENTITIES', "UTF-8"); 
                        else $encoded = $contents;
                        $doc = new DOMDocument();
@@ -781,4 +781,4 @@
        $string = preg_replace("/<[^\/>]*>([\s]?)*<\/[^>]*>/", ' ', $string); // remove empty tags
        return $string;
 }
-?>
\ No newline at end of file
+?>

See also

(Official Wordpress) Plugin Directory: Import HTML Pages http://wordpress.org/extend/plugins/import-html-pages/

Leary (plugin author): Sillybean: http://sillybean.net/code/wordpress/html-import/

Source code of Import HTML Pages 1.21 (suitable for this patch) http://downloads.wordpress.org/plugin/import-html-pages.1.21.zip

Last modified: 2010-11-24. Permanent url: http://www.iki.fi/karvinen/empty_files_handling_for_wordpress_html_importer.html

Tero Karvinen www.iki.fi/karvinen - Top - Validate HTML - Validate CSS