[英]PHP Delete all files except the following
I've got this script working to delete all files but leave just the selected ones, How can i change it so on the .exe file it catches any filename? 我有这个脚本工作删除所有文件但只保留选定的文件,如何更改它以便在.exe文件中捕获任何文件名?
$leave_files = array(''.$filename.'.exe', ''.$filename.'.nfo', ''.$filename.'.sfv');
foreach( glob("$foldername/*") as $file ) {
if( !in_array(basename($file), $leave_files) ) {
unlink($file);
}
for the .exe file i want it to capture ANY filename 对于.exe文件,我希望它捕获任何文件名
Thanks 谢谢
$leave_files = array('' . $filename . '.exe', '' . $filename . '.nfo', '' . $filename . '.sfv');
foreach (glob("$foldername/*") as $file) {
if (!in_array(basename($file), $leave_files)) {
if (substr($file, -4) == ".exe") {
continue;
} else {
unlink($file);
}
}
}
You can check if the last 4 signs of the filename are ".exe" and then don't delete them. 您可以检查文件名的最后4个符号是否为“.exe”,然后不删除它们。 (or delete them, depends on what you mean) (或删除它们,取决于你的意思)
if(substr($filename,-4) == ".exe") if(substr($ filename,-4)==“。exe”)
Check the extension: 检查扩展名:
if(!in_array(basename($file), $leave_files) && pathinfo($file, PATHINFO_EXTENSION) != 'exe') {
Or exclude them in the glob()
: 或者在glob()
排除它们:
foreach(array_diff(glob("$foldername/*"), glob("$foldername/*.exe") as $file) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.