A
A
Alex2019-12-16 15:08:22
PHP
Alex, 2019-12-16 15:08:22

Delete files with Ubuntu terminal from PHP?

Unable to delete files in upload folder via terminal:

$output2 = exec('rm -f /usr/share/nginx/html/upload/*.{jpg,png} 2>&1',$output);

output and output2 are empty - no errors. However, the files are not deleted. All files and folders ( according to ls -l ) are owned by www-data , php runs from www-data and according to this logic everything should work.
What is my mistake when working with exec and terminal, please tell me?!
p/s Through the terminal kitty from under the root, this command works.
..p/s foreach glob unlink don't suggest. You need a terminal.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Chesnokov, 2019-12-16
@cesnokov

Why don't you want to delete files using PHP, like this:

function remove_files( $dirname ) {
   $type = array( 'jpg', 'jpeg', 'png' );
   if ( is_dir($dirname) ) { $dir_handle = opendir( $dirname ); }
   if ( !$dir_handle ) { return false; }
   while( $file = readdir( $dir_handle ) ) {
      $ext = explode( '.', $file );
      if( !in_array( end( $ext ), $type ) ) { continue; }
      if ( $file != '.' && $file != '..' && !is_dir( $dirname . '/' . $file )) {
         unlink( $dirname . '/' . $file );
      }
   }
   closedir($dir_handle);
   return true;
}
remove_files( '/usr/share/nginx/html/upload' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question