Usage Analysis of thinkphp File Processing class Dir. class. php

Source: Internet
Author: User
Tags delete cache
This article mainly introduces thinkphp File Processing class Dir. class. the usage of php is analyzed as an example based on the file processing class Dir. class. the use of the php custom function del is a very practical technique.

This article mainly introduces thinkphp File Processing class Dir. class. the usage of php is analyzed as an example based on the file processing class Dir. class. the use of the php custom function del is a very practical technique.

This article analyzes the usage of thinkphp File Processing class Dir. class. php. Share it with you for your reference. The specific analysis is as follows:

In my WBlog, a cache cleanup worker can delete cache files generated when the program is running. All these files are stored in the Runtime folder of the project, at that time, when I used a function customized by the project function library to delete the cached files, I could only delete the entire Runtime. It was too rough. I think it is necessary to perform a further deletion, in fact, the thinkphp extension class library has a good file processing class Dir. class. php, Dir. class. php class, not all thinkphp versions are available. if the version you downloaded does not exist, you can find it from other versions. Now let's talk about Dir. class. php applications.

Dir. class. php is a file processing class. You can use it:

1. Get the file information under the Directory

2. delete a directory or file

As I want to improve the WBlog cache clearing function, I only write the above 2nd functions. As for the 1st functions, I will write the template text modification function again, the following is a del () method defined by the Controller that is clearing the cache:

The Code is as follows:

Public function del (){
$ Type = trim ($ _ GET ['type']);
If (emptyempty ($ type) $ this-> error ('select the cache type! ');

Switch ($ type ){
Case 1: // clear all
$ Path = WEB_PATH. 'runtime ';
Break;
Case 2: // file cache directory
$ Path = WEB_PATH. 'runtime/Temp ';
Break;
Case 3: // data directory
$ Path = WEB_PATH. 'runtime/Data/_ fields ';
Break;
Case 4: // Template File Cache
$ Path = WEB_PATH. 'runtime/cache ';
Break;
Case 5: // clear all background caches
$ Path = APP_PATH. 'runtime ';
Break;
Case 6: // Background File cache directory
$ Path = APP_PATH. 'runtime/Temp ';
Break;
Case 7: // background data directory
$ Path = APP_PATH. 'runtime/Data/_ fields ';
Break;
Case 8: // Template File Cache
$ Path = APP_PATH. 'runtime/cache ';
Break;
}

Import ("@. ORG. Dir"); // load the Dir. class. php class (I put it in the background Project)

If (! Dir: isEmpty ($ path) {// call isEmpty () of Dir. class. php statically ()
Dir: del ($ path );

$ This-> success ();
} Else {
$ This-> error ('cleared! ');
}
}


Note:

Import ("@. ORG. Dir") -- load the Dir. class. php class (I put it in the background Project)

Dir: isEmpty ($ path) -- call the isEmpty () of Dir. class. php statically ()

Dir: del ($ path); -- call the del () method of Dir. class. php statically.

I used static call methods when I used classes, which saves the trouble of Instantiation.

I hope this article will help you design PHP programs based on the ThinkPHP framework.

,

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.