OSDN Git Service

Handle error caused by reading empty file
authorgoldenapples <goldenapplesdesign@gmail.com>
Wed, 13 Feb 2013 19:23:45 +0000 (11:23 -0800)
committergoldenapples <goldenapplesdesign@gmail.com>
Wed, 13 Feb 2013 19:23:45 +0000 (11:23 -0800)
`fread()` will throw an error if trying to read an empty file (ie if
user deleted all post content or quit the editor on `wp post create`.
Detect that and fail gracefully.

php/utils.php

index 68cc356..bb0ee3d 100644 (file)
@@ -280,8 +280,13 @@ function launch_editor_for_input( $input, $title = 'WP-CLI' ) {
 
        \WP_CLI::launch( "\${EDITOR:-vi} '$tmpfile'" );
 
+       $filesize = filesize( $tmpfile );
+
+       if ( $filesize === 0 )
+               return false;
+
        $handle = fopen( $tmpfile, 'r' );
-       $output = fread( $handle, filesize( $tmpfile )  );
+       $output = fread( $handle, $filesize );
        fclose( $handle );
        unlink( $tmpfile );