For SPL, in addition to the various data structures and iterators we learned about earlier, another very useful feature is manipulation of files. We’re going to look at that today, and it’s the last part of the SPL series.
File operations
For PHP file operations, by default we use procedural functions such as fopen(), fwrite(), fread(), and fgets(). The SPL provides an object-oriented approach to these file-related operations.
File information class
The first thing we’re going to look at is the SplFileInfo class.
$file = new SplFileInfo(PHP SPL extension library (4) PHP './6.);
Copy the code
When you instantiate it, you need to pass a file path parameter. Here, for example, we’ll go straight to the test code from the previous article. Our $file variable then gets a SplFileInfo object, in which we can use a number of methods similar to file functions.
var_dump($file->getBasename());
// string(39) "6.php SPL extension library (4) function.php"
var_dump($file->getPathname());
// string(41) "./ 6.php SPL extension library (4) function.php"
var_dump($file->getFilename());
// string(39) "6.php SPL extension library (4) function.php"
var_dump($file->getRealPath());
/ / string (102) "/ Users/zhangyue MyDoc/blog post/dev blog/PHP / 2021/01 / source / 6. PHP SPL extensions library function. (4) PHP"
var_dump($file->getPathInfo());
// object(SplFileInfo)#2 (2) {
// ["pathName":"SplFileInfo":private]=>
// string(1) "."
// ["fileName":"SplFileInfo":private]=>
// string(1) "."
/ /}
var_dump($file->getFileInfo());
// object(SplFileInfo)#2 (2) {
// ["pathName":"SplFileInfo":private]=>
// string(41) "./ 6.php SPL extension library (4) function.php"
// ["fileName":"SplFileInfo":private]=>
// string(39) "6.php SPL extension library (4) function.php"
/ /}
Copy the code
We can use the above test code to get some path information about the file. Is it very similar to ordinary file manipulation functions, or is it named and used exactly the same way?
var_dump($file->getExtension());
// string(3) "php"
var_dump($file->getType());
// string(4) "file"
var_dump($file->getCTime());
// int(1611017967)
var_dump($file->getOwner());
// int(501)
var_dump($file->getGroup());
// int(20)
var_dump($file->getSize());
// int(3543)
Copy the code
Of course, these attributes of the file are also available, such as the file extension, type, time, owner, owner group, size and so on.
var_dump($file->isReadable());
// bool(true)
var_dump($file->isWritable());
// bool(true)
var_dump($file->isDir());
// bool(false)
var_dump($file->isFile());
// bool(true)
var_dump($file->isLink());
// bool(false)
Copy the code
Of course, there are ways to determine whether a file is writable, readable, a directory or file, or a connection. In general, the functions of ordinary file manipulation functions exist here, and we only list part of them here. More methods can be consulted in official files.
File manipulation class
In SplFileInfo, you don’t need to read or write files. Don’t worry, for other operations on files, we use another class, SplFileObject. It inherits from SplFileInfo and implements two more iterator interfaces. The functionality of these two multi-implemented interfaces will be covered later. SplFileInfo, on the other hand, extends some of the methods used to read and write files, making it easier to read and write files.
$txt1 = new SplFileObject('7.1. TXT'.'a+');
$txt1->fwrite(date('Y-m-d H:i:s' . PHP_EOL));
// 71.txt
/ / the 2021-01-20 09:03:15
/ /...
/ /...
Copy the code
At instantiation time, we specify the form of the file to be opened with the second argument to SplFileObject, which has the same effect as the related argument to fopen(). Here we use a+, which is the ability to append reads and writes. Because we’re constantly testing, we use a read/write property like this. After instantiation, we can write to the file normally using the object’s fwrite() method.
$txt1->seek(0);
var_dump($txt1->fread($txt1->getSize()));
// string(80) "2021-01-20 09:03:15
// "
Copy the code
For reading, we point the handle pointer inside the file object back to the beginning. The seek() method used here does not look familiar. Yes, SplFileObject implements the Seekable interface. Next, we use fread() to read the entire contents of the file.
Of course, the classic eof + fgets traversal is supported in SplFileObject as well as fgeTC and fgetss.
$txt1->seek(0);
while(!$txt1->eof()){
var_dump($txt1->fgets());
}
// string(20) "2021-01-20 09:03:15
// "
// string(20) "2021-01-20 09:03:16
// "
/ /...
/ /...
Copy the code
As mentioned earlier, SplFileObject inherits some iterator interface, which means that we can simply iterate over the object to get the contents of the file.
foreach($txt1 as $t){
var_dump($t);
}
// string(20) "2021-01-20 09:03:15
// "
// string(20) "2021-01-20 09:03:16
// "
/ /...
/ /...
Copy the code
Isn’t that amazing? This is really one of the magic things about SplFileObject. Foreach () is an eOF +fgets (foreach +fgets). Foreach () is an eof+fgets (foreach +fgets).
We’ve learned about common procedural file manipulation functions in previous articles, but most of them are implemented in SplFileInfo and SplFileObject, so we won’t demonstrate them here. If you have any questions about the file manipulation function, you can review the previous article again:
-
Function in PHP file system (a) : mp.weixin.qq.com/s/T7771zfSc…
-
In PHP file system function (2) : mp.weixin.qq.com/s/8THIW4du-…
-
In PHP file system function (3) : mp.weixin.qq.com/s/DU8ImuVYC…
Temporary file class
The last file-related operation we will look at is the temporary file operation. Remember we learned before the operation temporary files in the PHP mp.weixin.qq.com/s/bktuy1XSQ… Similarly, there are classes in the SPL library that operate on temporary files.
$tmp = new SplTempFileObject(0);
$tmp->fwrite("tmp:" . date('Y-m-d H:i:s'));
$tmp->rewind();
foreach ($tmp as $line) {
var_dump($line);
}
// string(23) "tmp:2021-01-20 09:14:34"
sleep(10);
// vim /tmp/phpRhgsVZ
// tmp:2021-01-20 09:14:34
Copy the code
Tmpfile () saves temporary files in the/TMP directory and deletes the files when the program is finished. Note, however, that our SplTempFileObject class passes a 0 when instantiated to indicate that it does not use memory as a temporary file directory, but is actually stored in the temporary file directory. If this parameter is left blank or a number greater than 0 indicates that the temporary file is stored in memory, and a number greater than 0 indicates the amount of memory used. If this parameter is left blank, the default value is 2MB.
Directory traversal iterator
The directory iterator is something we didn’t cover in our previous talk about iterators, and something we said we would cover in our article on file directories. Actually this aspect of the content we also studied in before, the PHP content access catalog of all the RecursiveDirectoryIterator mp.weixin.qq.com/s/W8jE-AXZI… This the RecursiveDirectoryIterator is used in the SPL recursive directory iterators. Today, we will learn a simple, is also one of the the RecursiveDirectoryIterator iterator superior iterators, is in the use of common directory iterator.
foreach (new DirectoryIterator('/') as $fileInfo) {
if($fileInfo->isDot()) continue;
if($fileInfo->isDir()){
echo "dir: " . $fileInfo->getFilename() , PHP_EOL;
}else{
echo "file: " . $fileInfo->getFilename() , PHP_EOL; }}// file: 2. Learn about the SeasLog extension in PHP
// dir: autoloadA
/ / file: 7.1. TXT
// file: 6.php SPL extension library (4).php
// file: 1.php some miscellaneous functions learn.php
// file: browscap.ini
// file: 7.php SPL extension library (4) file and design patterns.php
// file: 3.php SPL extension library (a) data structure.php
// file: 4.php SPL extension library (2) Object array and array iterator.php
// file: 1.txt
// dir: autoloadB
// file: 5.php SPL extension library (3) iterator.php
Copy the code
Also very simple code, not explained here. The differences between recursion-enabled iterators are the same as those of other iterators, and are explained in previous articles.
Design patterns
Finally, let’s review design patterns. The Design Patterns series is a series of articles written earlier. Some of the design patterns prepared for us and implemented in SPL were covered in two articles at that time.
The iterator
Iterator believe has been needless to say, the previous article the SPL extension of PHP library (3) the iterator mp.weixin.qq.com/s/KevUTCbfm… The implementation of various iterators in SPL has been described in detail. For the iterator this design pattern, also can refer to our previous article “the iterator pattern of PHP design patterns” mp.weixin.qq.com/s/uycac0OXY… Learn more about it. Iterator is not an SPL extension. There are just a few different concrete iterator implementations in the SPL library.
The observer
In addition to iterators, the other is the observer pattern. SplObserver and SplSubject are two classes that are directly available in the SPL library to implement the observer pattern. Specific implementation code before and we are in the interpretation of this design pattern design pattern series of articles have detailed instructions, you can in the past study well under the observer pattern of PHP design pattern mp.weixin.qq.com/s/SlSToMIGN… .
conclusion
One thing you can see from this article is that the SPL extension library is now very common, otherwise there wouldn’t be so many links to previous articles. We believe that in the future business development, we will use more classes in SPL to operate files, which is also a trend, after all, object-oriented method is more mainstream. Well, the end of another big extension, the SPL library as a default integration into the official extension, is really worth a lot of time to learn, learn this extension will not disappoint you!
Test code:
Github.com/zhangyue050…
Reference Documents:
www.php.net/manual/zh/b…