PHP Delete log files

Source: Internet
Author: User
Tags filetime
Just now I have a folder to store the log, what do I need to do to use the program to delete the older log files? Do the great gods have a way?


Reply to discussion (solution)

foreach (Glob (' Log file directory/*.* ') as $FN) {  if (Filemtime ($FN) < specified time) unlike ($FN);}

Can write a script to delete, run once a day check expiration (compared to the current time and the last modified time of the log or creation time) to delete the log file

Script words such as this, delete content modified more than 7 days of the file

is to use PHP to complete this work, that is, I have Baidu to provide the folder inside the file according to the time to sort, but I use it is not possible ...
This is my Code
!--? php
function dir_size ($dir, $url) {
$dh = @opendir ($dir);//Open Directory, return a directory stream
$return = Array ();
$i = 0;
while ($file = @readdir ($DH)) {//loop to read files under directory
if ($file! = '. ' and $file! = ' ... ') {
$path = $dir. '/'. $file;//set directory to include subdirectories
if (Is_dir ($path)) {
}elseif (Is_file ($path)) {
$filesize [] = Round ((filesize ($path)/1024), 2);//Gets the file size
$filename [] = $path;//Gets the file name
$filetime [] = Date ("Y-m-d h:i:s", Filemtime ($path));//Gets the file last modified date
$return [] = $url. ' /'. $file;
}
}
}
@closedir ($DH);//Close Directory Flow
Array_multisort ($filetime, sort_desc,sort_string, $files);//Sort by Time return $return; Returns the file
}
Dir_size ("Htdocs", "Htdocs");

?>
Running after a problem
Array_multisort (): Argument #1 is expected to being an array or a sort flag
The best result is I want P HP can arrange files in chronological order, so that I can selectively delete files by using functions ...

Have studied ....

Array_multisort ($filetime, sort_desc,sort_string, $files);//Sort by time
Where are $files assigned values?

Array_multisort ($filetime, sort_desc,sort_string, $files);//Sort by time
Where are $files assigned values?

I checked the use of the function, I know the use of the Array_multisort function, the last argument should be an array, but I do not know how to assign a value to the fourth parameter. $files should refer to all the files in the file, I don't know how to use it. $files = $return [] = $url. ' /'. $file;?

I changed the $files into a $return or no use ...

function can be rewritten as

function Dir_size ($dir, $url) {  $DH = @opendir ($dir);             Open Directory, return a directory stream  $return = array ();  $i = 0;  while ($file = @readdir ($DH)) {     //loop read the file under the directory    if ($file! = '. ' and $file! = ' ... ') {      $path = $dir. '/'. $file;     Set the directory for the case that contains subdirectories      if (Is_dir ($path)) {      }elseif (Is_file ($path)) {        $files [] = array (          ' filename ' = > $path,//Get the file name                               ' filesize ' = Round ((filesize ($path)/1024), 2),//Get the file size          ' filetime ' and ' = Date ' (" Y-m-d h:i:s ", Filemtime ($path)),//Get file Last Modified date             ' return ' = $url. ' /'. $file          );        $filetime [] = Filemtime ($path),//Get file Last modified date}}}    @closedir ($DH);             Close directory stream  array_multisort ($filetime, sort_desc,sort_string, $files);//Sort by Time  return $files;               Return file}

NN, I used your code to find out what I could use.
Then I found that there is another way to put $files directly to replace the $return, because the previous data are in return.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.