Multiple file removal in PHP -


hello , in advance! thing have 2 files named 'test3.txt' , 'text2.txt' in folder. thing want remove both of them storing them in array. firstly check if files exist 'file_exists' method , when try remove them fails. know doing wrong did little research , couldn't find answer problem. trying remove these both files @ once if it's possible somehow.

1  <?php 2  3  $files = array('test3.txt', 'text2.txt'); 4  $exists = false; 5   6  foreach ($files $file) { 7      if (file_exists($file)) { 8      $exists = true; 9      } 10 } 11  12 if ($exists == true) { 13     unlink($files); 14     echo "files deleted"; 15 } 16     else { 17         echo "couldn't delete files"; 18     } 

browser returns true though files still aren't removed directory. here's browser output:

 warning: unlink() expects parameter 1 valid path, array given in /var/www/html/web/copy.php on line 13 files deleted 

the documentation of unlink() functions says expects string first argument, filename. not array.

see http://php.net/manual/en/function.unlink.php

you try instead of manually iterating on files:

<?php $files = ['test3.txt', 'text2.txt']; array_map('unlink', $files); 

(just tried myself... works!)


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -