@@ -285,10 +285,27 @@ protected function sqlite_export( $file, $assoc_args ) {
285285 WP_CLI ::error ( 'Database does not exist. ' );
286286 }
287287
288+ if ( ! $ this ->is_sqlite3_available () ) {
289+ WP_CLI ::error ( 'The sqlite3 binary could not be found. Please install sqlite3 to use the export command. ' );
290+ }
288291 $ temp_db = tempnam ( sys_get_temp_dir (), 'temp.db ' );
292+ if ( false === $ temp_db ) {
293+ WP_CLI ::error ( 'Could not create temporary database file for export. ' );
294+ }
295+
289296 $ export_db = tempnam ( sys_get_temp_dir (), 'export.db ' );
297+ if ( false === $ export_db ) {
298+ // Clean up any previously created temporary database file.
299+ @unlink ( $ temp_db );
300+ WP_CLI ::error ( 'Could not create temporary export file. ' );
301+ }
290302
291- copy ( $ db_path , $ temp_db );
303+ if ( ! @copy ( $ db_path , $ temp_db ) ) {
304+ // Clean up temporary files if the copy operation fails.
305+ @unlink ( $ temp_db );
306+ @unlink ( $ export_db );
307+ WP_CLI ::error ( 'Could not copy database to temporary file for export. ' );
308+ }
292309
293310 $ exclude_tables = [];
294311
@@ -436,18 +453,18 @@ protected function sqlite_import( $file, $assoc_args ) {
436453 }
437454
438455 $ command_parts [] = $ db_path ;
456+ $ command_parts [] = '.read ' ;
457+ $ command_parts [] = $ import_file ;
439458
440- $ command = Utils \esc_cmd (
459+ // Build debug-friendly command string without using shell redirection.
460+ $ debug_command = Utils \esc_cmd (
441461 implode ( ' ' , array_fill ( 0 , count ( $ command_parts ), '%s ' ) ),
442462 ...$ command_parts
443463 );
444464
445- $ command .= ' < ' . escapeshellarg ( $ import_file );
446-
447- WP_CLI ::debug ( "Running shell command: {$ command }" , 'db ' );
448-
449- $ result = \WP_CLI \Process::create ( $ command , null , null )->run ();
465+ WP_CLI ::debug ( "Running shell command: {$ debug_command }" , 'db ' );
450466
467+ $ result = \WP_CLI \Process::create ( $ command_parts , null , null )->run ();
451468 unlink ( $ import_file );
452469
453470 if ( 0 !== $ result ->return_code ) {
0 commit comments