I need to delete old files with special characters in filenames like space,,
,(
,)
,!
and so on via PHP. Classic unlink($filename)
does not work for these files. How can I transform filenames to filenames which accepts unlink function and filesystem? It's running on a Solaris machine and I don't have another access to it.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
How are you constructing the
$filename
? unlink should work on any filename with special characters if you do the normal escaping on it. e.g.for a file with a name of
this has, various/wonky !characters in it
, thenshould work.
You can also find all needed files and unlink them using RegexIterator:
This code snippet recursively traverse all directories from current ('.') and matches all files by regex '/(^.[\s\,.()!]+.)/', then delete them.
unlink accepts any valid string and will try to delete the file in that string.
Perhaps you are not properly escaping certain characters.
The way I do it in Linux is to use absolute paths, like "rm ./filename" that is dot slash.
You can also use escape charachters. Like "rm \-filename" that is - backspash hyphen.