@@ -359,6 +359,13 @@ protected function sqlite_export( $file, $assoc_args ) {
359359 }
360360
361361 $ init_file = tempnam ( sys_get_temp_dir (), 'export_init ' );
362+ if ( false === $ init_file ) {
363+ if ( file_exists ( $ temp_db ) ) {
364+ unlink ( $ temp_db );
365+ }
366+
367+ WP_CLI ::error ( 'Failed to create temporary SQLite init file for export. ' );
368+ }
362369 $ init_file = str_replace ( '\\' , '/ ' , $ init_file );
363370
364371 $ init_contents = '' ;
@@ -367,14 +374,25 @@ protected function sqlite_export( $file, $assoc_args ) {
367374 }
368375 $ init_contents .= ".dump \n" ;
369376
370- file_put_contents ( $ init_file , $ init_contents );
377+ $ bytes_written = file_put_contents ( $ init_file , $ init_contents );
378+ if ( false === $ bytes_written ) {
379+ if ( file_exists ( $ init_file ) ) {
380+ unlink ( $ init_file );
381+ }
382+ if ( file_exists ( $ temp_db ) ) {
383+ unlink ( $ temp_db );
384+ }
385+ WP_CLI ::error ( 'Could not export database ' );
386+ }
371387
372388 $ command = Utils \esc_cmd ( 'sqlite3 -init %s %s .exit ' , $ init_file , $ temp_db );
373389
374390 WP_CLI ::debug ( "Running shell command: {$ command }" , 'db ' );
375391
376392 $ result = \WP_CLI \Process::create ( $ command , null , null )->run ();
377- unlink ( $ init_file );
393+ if ( file_exists ( $ init_file ) ) {
394+ unlink ( $ init_file );
395+ }
378396
379397 if ( 0 !== $ result ->return_code ) {
380398 if ( file_exists ( $ temp_db ) ) {
@@ -449,8 +467,18 @@ protected function sqlite_import( $file, $assoc_args ) {
449467 $ contents = preg_replace ( '/\bCREATE UNIQUE INDEX (?!IF NOT EXISTS\b)/i ' , 'CREATE UNIQUE INDEX IF NOT EXISTS ' , (string ) $ contents );
450468
451469 $ import_file = tempnam ( sys_get_temp_dir (), 'temp.db ' );
470+ if ( false === $ import_file ) {
471+ WP_CLI ::error ( 'Failed to create a temporary file for SQLite import. ' );
472+ }
473+
452474 $ import_file = str_replace ( '\\' , '/ ' , $ import_file );
453- file_put_contents ( $ import_file , $ contents );
475+ $ bytes_written = file_put_contents ( $ import_file , $ contents );
476+ if ( false === $ bytes_written ) {
477+ if ( file_exists ( $ import_file ) ) {
478+ unlink ( $ import_file );
479+ }
480+ WP_CLI ::error ( sprintf ( 'Failed to write SQLite import data to temporary file: %s ' , $ import_file ) );
481+ }
454482
455483 // Build sqlite3 command.
456484 $ command_parts = [ 'sqlite3 ' ];
0 commit comments