File action functions
1, Get filename: basename ();
2, get the file directory: DirName ();
3, PathInfo () Get the file information, return the result is an array, including path, file full name, file name and extension. For example:
The code is as follows |
Copy Code |
$file = '/com/netingcn/error.log '; Print_r (PathInfo ($file)); The results are: Array ( [DirName] =>/COM/NETINGCN [BaseName] => Error.log [Extension] => log [FileName] => error ) |
4, to determine whether the document exists: Is_file ();
5, to determine whether the directory exists: Is_dir ();
6, to determine whether the file or directory exists: File_exists ();
7. Read all file contents: file () or file_get_contents (), where file () returns a row for the element of Array,file_get_contents () to return all the contents of the file as a string;
8, write file fwrite, such as:
The code is as follows |
Copy Code |
$handler = fopen ($file, ' w '); W will flush out the previous content, A is append Fwrite ($handler, ' content '); Fclose ($handler); Remember to close open file handle 9, there are many file read operations, |
Here is a brief introduction to several:
The code is as follows |
Copy Code |
$handler = fopen ($file, ' R '); while (!feof ($handler)) { $datas [] = Fgets ($handler); Read one line of content } while (!feof ($handler)) { $datas [] = FGETSS ($handler); Read one line and come over HTML tag } while (!feof ($handler)) { $datas [] = Fgetcsv ($handler); Read one line of content and parse CSV field } $content = Fread ($handler, $strLength); Reads the specified long read character Fclose ($handler); |
PHP Read file action function
1. Use Fread () to obtain
Please look at the following PHP code:
The code is as follows |
Copy Code |
<?php $file = "Phpddt.txt"; $fp = fopen ($file, "R"); if ($fp) { while (!feof ($fp)) { The second parameter is the length of the read $data = Fread ($fp, 1000); } Fclose ($FP); } Echo $data; ?> |
Run Result:
PHP Point-and-click (www.111cn.net), Focus on PHP development, to provide professional PHP tutorials!
2.fseek (resource handle, int offset [, int whence]), offset the pointer to offset.
(Php.txt content is "Welcome to Www.111cn.net")
After running the following PHP code:
The code is as follows |
Copy Code |
<?php $file = "Php.txt"; $fp = fopen ($file, "R"); After you jump the file pointer to the 8th byte Fseek ($FP, 8); Reading data $data = Fgets ($fp, 4096); Echo $data; ?>
|
The results are:
To Www.111cn.net
The whence parameters are described as follows:
Seek_set-Set position equals offset byte.
Seek_cur-Set position plus offset for current position.
Seek_end-Sets the position for the end of the file plus offset. (Assignment)
If whence is not specified, the default is Seek_set.
3.ftell () function to get the offset of the pointer position
The PHP demo code is as follows:
The code is as follows |
Copy Code |
<?php $file = "Phpddt.txt"; $fp = fopen ($file, "R"); After you jump the file pointer to the 8th byte Fseek ($FP, 8); Gets the offset of the pointer position Echo Ftell ($FP); ?>
|
Run Result:
8
The 4.rewind () function moves the file pointer to the specified location
5.parse_ini_file () function, parse. ini file, easily parse multidimensional array. Look at the following PHP tutorial to understand!
First, save the Phpddt.ini file, which reads as follows:
[Web1]
Url= "Www.111cn.net"
name = php dot-dot Pass
[WEB2]
Url= "Www.baidu.com"
Name = Baidu Search
Write the following PHP code:
The code is as follows |
Copy Code |
<?php $file _arr = Parse_ini_file ("Phpddt.ini", true); Print_r ($file _arr); ?>
|
The results of the operation are as follows:
Array
(
[Web1] => Array
(
[url] => www.111cn.net
[name] => php dot-dot Pass
)
[WEB2] => Array
(
[url] => www.baidu.com
[Name] => Baidu Search
)
)
Directory Operations
The first is a function read from the directory, Opendir (), Readdir (), Closedir (), which is used to open the file handle first and then iterate over the following list:
The code is as follows |
Copy Code |
<?php $base _dir = "filelist/"; $fso = Opendir ($base _dir); echo $base _dir. " while ($flist =readdir ($FSO)) { echo $flist. " <br/> "; } Closedir ($FSO) ?> |
This is a program that returns the files in the directory below the file directory (0 files will return false).
Sometimes you need to know the directory information, you can use the DirName ($path) and basename ($path), respectively, to return the directory section of the path and file name part, available disk_free_space ($path) back to see space space.
To create a command:
The code is as follows |
Copy Code |
mkdir ($path, 0777) |
, 0777 is the permission code, and the Umask () function can be set under non window.
RmDir ($path)
The path will be deleted in the $path file.
DIR-The Directory class is also an important class for manipulating file directories, with 3 methods, Read,rewind,close, which is an object-oriented class that first uses open file handles and then reads with pointers. Here's a look at the PHP manual:
The code is as follows |
Copy Code |
<?php $d = Dir ("/etc/php5"); echo "Handle:". $d->handle. "N"; echo "Path:". $d->path. "N"; while (false!== ($entry = $d->read ())) { echo $entry. " n "; } $d->close (); ?> |
Output:
Handle:resource ID #2
Path:/ETC/PHP5
.
..
Apache
Cgi
Cli
The properties of the file are also important, including the creation time, the last modification time, the owner, the filegroup, the type, the size, and so on.
Here we focus on file operation.
Third, file operation
Read files
First is a file to see whether can read (permission problem), or exist no, we can use the Is_readable function to get information.:
The code is as follows |
Copy Code |
<?php $file = ' dirlist.php '; if (is_readable ($file) = = False) { Die (' File not present or unreadable '); } else { echo ' existence '; } ?> |
The function to determine the existence of a file is also file_exists (shown below), but this is clearly not is_readable comprehensive. When a file exists, you can use
The code is as follows |
Copy Code |
<?php $file = "filelist.php"; if (file_exists ($file) = = False) { Die (' File not present '); } $data = file_get_contents ($file); echo htmlentities ($data); ?> |
However, the file_get_contents function is not supported on a lower version, you can first create a handle to the file and then read it all with the pointer:
$fso = fopen ($cacheFile, ' R ');
$data = Fread ($fso, FileSize ($cacheFile));
Fclose ($FSO);
There is also a way to read binary files:
$data = Implode ("", File ($file));
Write a file
and read the file in the same way, first see if you can write:
The code is as follows |
Copy Code |
<?php $file = ' dirlist.php '; if (is_writable ($file) = = False) { Die ("I am chicken feathers, I cannot"); } ?> |
If you can write it, you can use the File_put_contents function to write:
The code is as follows |
Copy Code |
<?php $file = ' dirlist.php '; if (is_writable ($file) = = False) { Die (' I'm chicken feathers, I can't '); } $data = ' I am contemptible, I want '; File_put_contents ($file, $data); ?> |
The newly introduced function of the file_put_contents function in PHP5 (if you don't know it, use the Function_exists function to determine first) the lower version of PHP is not available, and you can use the following methods:
$f = fopen ($file, ' w ');
Fwrite ($f, $data);
Fclose ($f);
Replaced.
When writing a file, you sometimes need to lock and then write:
The code is as follows |
Copy Code |
function Cache_page ($pageurl, $pagedata) { if (! $fso =fopen ($pageurl, ' W ')) { $this->warns (' Unable to open cache file. '); /trigger_error return false; } if (!flock ($fso, lock_ex)) {//LOCK_NB, exclusive lock $this->warns (' Unable to lock cache file. '); /trigger_error return false; } if (!fwrite ($fso, $pagedata)) {//write byte stream, serialize write to other format $this->warns (' Unable to write cache file. '); /trigger_error return false; } Flock ($fso, Lock_un);/release lock Fclose ($FSO); return true; } |
copying, deleting files
PHP Delete file is very easy to use the unlink function simple operation:
The code is as follows |
Copy Code |
<?php $file = ' dirlist.php '; $result = @unlink ($file); if ($result = = False) { echo ' mosquito away '; } else { Echo ' cannot be driven away '; } ?> |
Can.
Copying files is also easy:
The code is as follows |
Copy Code |
<?php $file = ' yang.txt '; $newfile = ' ji.txt '; # This file parent folder must be able to write if (file_exists ($file) = = False) { Die (' sample not on-line, cannot copy '); } $result = Copy ($file, $newfile); if ($result = = False) { echo ' Copy memory OK '; } ?> |
You can use the rename () function to rename a folder. Other operations are a combination of these functions can be achieved.
Get file properties
I say a few common functions:
Get the last modified time:
The code is as follows |
Copy Code |
<?php $file = ' test.txt '; echo Date (' R ', Filemtime ($file)); ?> |
Returns the Unix-time timestamp, which is commonly used in caching techniques.
Related also to get last accessed time Fileatime (), Filectime () When file permissions, owner, all groups or other inode metadata is updated, Fileowner () function returns file owner
$owner = Posix_getpwuid (Fileowner ($file));
(Non-Window System), Ileperms () Gets the permissions of the file,
The code is as follows |
Copy Code |
<?php $file = ' dirlist.php '; $perms = substr (sprintf ('%o ', Fileperms ($file)),-4); Echo $perms; ?> |
FileSize () returns the number of bytes in the file size:
The code is as follows |
Copy Code |
<?php Output similar to: somefile.txt:1024 bytes $filename = ' somefile.txt '; Echo $filename. ': ' . FileSize ($filename). ' bytes '; ?> |
Get all the information for a file there is a function stat () function that returns an array:
code is as follows |
copy code |
<?php $file = ' dirlist.php '; $perms = stat ($file); Var_dump ($perms); ? |