2010-06-27 13 views
23

मैं इस php पटकथा लिखी 24 घंटे, से अधिक उम्र के पुराने फाइलों को डिलीट करना हट जाता है लेकिन यह नये सहित सभी फ़ाइलें हटा दी:PHP स्क्रिप्ट, सभी फाइलों

<?php 
    $path = 'ftmp/'; 
    if ($handle = opendir($path)) { 
    while (false !== ($file = readdir($handle))) { 
     if ((time()-filectime($path.$file)) < 86400) { 
      if (preg_match('/\.pdf$/i', $file)) { 
       unlink($path.$file); 
      } 
     } 
    } 
    } 
?> 
+0

आप किस ओएस का उपयोग कर रहे हैं? Win32 या यूनिक्स/लिनक्स? –

+9

यह नहीं होना चाहिए> 86400? –

+0

यह एक लिनक्स सिस्टम पर है। मुझे मेरी त्रुटि दिखाई देती है। लेकिन यह पुरानी फाइलों को भी क्यों हटा दिया? – ChuckO

उत्तर

28
(time()-filectime($path.$file)) < 86400 

वर्तमान समय और फाइल की बदली हुई समय की एक दूसरे को, तो भीतर 86400 सेकंड हैं ...

if (preg_match('/\.pdf$/i', $file)) { 
    unlink($path.$file); 
} 

मुझे लगता है कि आपकी समस्या हो सकती है। इसे> या> = में बदलें और इसे सही तरीके से काम करना चाहिए।

8
  1. आप इसके बजाय > चाहते हैं।
  2. जब तक आप विंडोज़ पर नहीं चल रहे हैं, तो आप इसके बजाय filemtime() चाहते हैं।
52
<?php 

/** define the directory **/ 
$dir = "images/temp/"; 

/*** cycle through all files in the directory ***/ 
foreach (glob($dir."*") as $file) { 

/*** if file is 24 hours (86400 seconds) old then delete it ***/ 
if(time() - filectime($file) > 86400){ 
    unlink($file); 
    } 
} 

?> 

तुम भी

jpg छवियों के लिए उपयोग करें (वाइल्डकार्ड) जैसे * के बाद एक विस्तार जोड़कर फ़ाइल प्रकार निर्दिष्ट कर सकते हैं: glob($dir."*.jpg")

txt फ़ाइलों के लिए उपयोग करें: glob($dir."*.txt")

htm के लिए फाइलें उपयोग: glob($dir."*.htm")

7
<?php 
$dir = getcwd()."/temp/";//dir absolute path 
$interval = strtotime('-24 hours');//files older than 24hours 

foreach (glob($dir."*") as $file) 
    //delete if older 
    if (filemtime($file) <= $interval) unlink($file);?> 
0

ठीक काम

$path = dirname(__FILE__); 
if ($handle = opendir($path)) { 
while (false !== ($file = readdir($handle))) { 
$timer = 300; 
$filetime = filectime($file)+$timer; 
$time = time(); 
$count = $time-$filetime; 
    if($count >= 0) { 
     if (preg_match('/\.png$/i', $file)) { 
     unlink($path.'/'.$file); 
     } 
    } 
} 
} 
संबंधित मुद्दे