2014-01-18 51 views
1

我想從名爲tempimages的文件夾中刪除大於1小時的所有圖像。 我發現對堆棧溢出下面的例子,但我得到解析錯誤:刪除所有大於1小時的圖像

syntax error, unexpected '{' in /delete-old-images.php on line 13

<?php 
function destroy($dir) { 
$mydir = opendir($dir); 
while($file = readdir($mydir)) { 
    if($file != "." && $file != "..") { 
     chmod($dir.$file, 0777); 
     if(is_dir($dir.$file)) { 
      chdir('.'); 
      while($dir.$file) { 
       if(date("U",filectime($file) >= time() - 3600) 
       { 
        unlink($dir.$file) 
       } 
      } 

     } 
     else 
      unlink($dir.$file) or DIE("couldn't delete $dir$file<br />"); 
    } 
} 
closedir($mydir); 
} 

destroy("tempimages/"); 
?> 

我的服務器數據:

PHP Version 5.3.18-nmm1 
System Linux #116-Ubuntu SMP Tue Nov 12 19:37:57 UTC 2013 x86_64 
Build Date Oct 26 2012 16:30:11 
Server API Apache 2.0 Handler 

如何解決呢?

回答

2

你忘了關date()

if(date("U",filectime($file)) >= time() - 3600) 
         -------^ 

這裏

unlink($dir.$file); 
      -----^ 
0

錯過了一個分號您還沒有關閉支架,如果條件。

if(date("U",filectime($file) >= time() - 3600) 

應該

if(date("U",filectime($file) >= time() - 3600)) 

,錯過了semicolumn

unlink($dir.$file) 

應該

unlink($dir.$file);