Delete files with special characters in file names

I need to remove the old files with special characters in file names, such as space, ,, (, ), !, etc. through PHP. Classic unlink($filename)does not work for these files. How to convert file names to file names that accept unlink and the file system? It runs on a Solaris computer and I have no other access to it.

+3
source share
4 answers

How do you build $filename? unlink should work with any file name with special characters if you are doing normal escaping. eg.

for a file named this has, various/wonky !characters in itthen

 $filename = 'this has\, various\/wonky \!characters in it';
 unlink($filename);

must work.

+2
source

unlink accepts any valid line and attempts to delete the file on that line.

unlink('/home/user1/"hello(!,');

Perhaps you are not avoiding certain characters.

+1
source

RegexIterator:

<?php

$dir  = new RecursiveDirectoryIterator('.');
$iterator = new RecursiveIteratorIterator($dir);
$regex = new RegexIterator($iterator, '/(^.*[\s\.\,\(\)\!]+.*)/', RecursiveRegexIterator::GET_MATCH);

foreach ($regex as $file) {
        if (is_file($file[0])) {
                print "Unlink file {$file[0]}\n";
                unlink($file[0]);
        }
}

('.') regex '/(^.[\s\,.()!]+.)/', .

+1

, Linux, - , "rm./filename", .

escape-. "rm\-filename", - backspash.

0

Source: https://habr.com/ru/post/1782646/


All Articles