![](/img/trans.png)
[英]How to delete all folders and files in a directory except a specific folder in PHP
[英]how delete All files except one in folder with php?
我有此php代碼以刪除文件夾中的所有圖像文件,但我需要保留example.php文件。
我怎么能?
$dir = @opendir('../result_image/');
while (false !== ($file = @readdir($dir)))
{
if ($file != '..' && $file != '.')
{
$upload[] = $file;
}
}
$time_sec=@time();
for($i=0;$i<count($upload);$i++)
{
$time_file=@filemtime('../result_image/'.$upload[$i]);
$time=$time_sec-$time_file;
if($time>9000)
{
//if ($upload[] = "disk_save.php") continue;
@unlink('../result_image/'.$upload[$i]);
}
}
我對自己的問題有3個答案,並且所有問題都可以解決。但是,最好的還是女巫? 用戶每次上傳圖片時都會執行此代碼。
我將按照以下步驟進行操作。
$time_sec=time();
$excludeFiles = array('example.php','disk_save.php');
for($i=0;$i<count($upload);$i++)
{
$time_file=filemtime('../result_image/'.$upload[$i]);
$time=$time_sec-$time_file;
if($time>9000)
{
try {
if (!in_array($upload[$i], $excludeFiles))
unlink('../result_image/'.$upload[$i]);
} catch (Exception $e) {
// Error handle here
}
}
}
您可以通過在unlink
功能之前添加一個簡單的if
條件,如下所示。
//Your code here
if($upload[$i] != 'example.php'){
unlink('../result_image/'.$upload[$i]);
}
注意:在函數中添加@
in-front不是一個好習慣,請嘗試克服所有bug(如果發生)。
分解存儲在上傳中的每個值。 我猜您擁有文件的全名:
$unwantedExt = array('php');
for($i=0;$i<count($upload);$i++) {
list($fileName, $extension) = explode('.', $upload[$i]);
if(!in_array($extension, $unwantedExt)) {
unlink('../result_image/'.$upload[$i]);
}
}
您可以使用glob()
遍歷目標目錄中的所有文件。 另外,如果您只想刪除(例如) *.png
文件,則可以將擴展名添加到glob()
調用中以提高效率(以及確保僅刪除提到的圖像文件) 。
<?php
$excluded = array('example.php', 'disk_save.php', 'some-other-file.php', ); //files to skip over, see comment below
//if all of the images you want to delete have the same extension, it would be wise to
//limit your iteration to only files with that extension:
// glob("../result_image/*.png")
//If you want to delete multiple extensions:
// glob("../result_image/*.{png,jpg}", GLOB_BRACE)
//Additionally, the `if (in_array(... continue;` statement becomes unnecessary and can be commented
//out or removed (assuming you added an extension to glob()).
foreach(glob("../result_image/*") as $fn) {
if (in_array(basename($fn), $excluded)) //skip over excluded files
continue; //see comment above
$time_sec = time(); //if you are not working with a large number of image files, this line should be
//moved out of the foreach loop, to reduce execution time.
$time_file = filemtime($fn);
//check age of $fn and remove if it is old enough to be deleted
if ($time_sec - $time_file > 9000) {
unlink($fn);
}
}
該代碼也對我有用,我將.php名稱文件放在IF中,然后再將另一個文件放在$ upload []中。 像這樣:
$dir = opendir('../result_image/');
while (false !== ($file = readdir($dir)))
{
if ($file != 'disk_save.php')
{
$upload[] = $file;
}
}
$time_sec=time();
for($i=0;$i<count($upload);$i++)
{
$time_file=filemtime('../result_image/'.$upload[$i]);
$time=$time_sec-$time_file;
if($time>9000)
{
//if($upload[$i] != 'disk_save.php'){
unlink('../result_image/'.$upload[$i]);
//}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.