1, directory operation
first is the function read from the directory, opendir (), readdir (), closedir (), when used first to open the file handle, and then iterate out:
[code].lt; php
$base_dir=.quot; filelist/.qu Ot;
$fso=opendir ($base_dir);
echo $base_dir..Quot;.Lt; hr/.gt;.Quot;
while. False).
sometimes you need to know the information of the directory, you can use dirname ($path) and basename ($path), return the directory part of the path and the name part of the file name, and use disk_free_space ($path) to check the space remaining space for the.
create command. Under indow, use the umask () function to set.
rmdir ($path): delete the path in the $path file.[/code]
two, the file operation
> to create a new file
first, determine the directory permission you want to create a new file; the device is 777. Then, the name of the new file is recommended to use the absolute path.
[code].lt;? Php
$filename=.quot; test.txt.quot;
$fp=fopen (.Quot; $filename.quot;.Quot; w.Quot;); / / / / / / / / > .quot;);
}
//fwrite ($filename,.Quot; anything you want to write to);
[code].lt;? Php
$file ='dirlist.php';
if (is_readable ($file) = = false) {
Obviously, there is no is_readable overall. When a file exists, you can use
[code].lt;? Php
$file =.Quot; filelist.php.quot;
if ($file) = =; E]
but file_get_contents function is not supported in a lower version, you can first create a handle of the file, then read all with pointers:
, and one way to read binary files:
[code]$data = implode ('', file ($file)); [/code]
- write file
and read.
[code].lt; php
$file ='dirlist.php';
if (is_writable ($file) = = false) Ode].lt;? Php
$file ='dirlist.php';
if (is_writable ($file) = = false) {
die (' I am chicken hair, I can't ');
[code]$f = fopen ($file,'w'),
fwrite ($f, $data),
[code]$f = fopen ($f, $data), sometimes need to be locked, and then write: On cache_page ($pageurl, $pagedata) {
if ($fso=fopen ($pageurl,'w')) {
$this-.gt; warns (' can not open caching file. ') //trigger_error
return false;
}
if (! Fwrite ($fso, $pagedata)) = / / write byte stream, serialize writes to other formats
$this-.gt;
Fclose ($fso);
return true;
}[/code]
replication, delete file
php delete files very much. The son is driven away ';
> else {
echo' can't be removed ';
}
?.gt; [/code]
can be a.
copy file is also easy:
$result = copy ($file, $newfile);
if ($result = = false) {
/ > can rename a folder using the function () function. The other operations are combined with these functions. .
* to get file properties
I say a few common functions:
get the latest modification time:
[code].lt;? Php
$file ='test.txt'; / > related also to get the time fileatime (), filectime (), the permission of the file, the owner, all groups, or the metadata in the other inode are updated, and the fileowner () function returns the file owner
$owner = posix_getpwuid (fileowner ($file));
(non window system), ileperms () gets the right to file
[code].lt;? Php
$file ='dirlist.php';
$perms = substr (sprintf ('%o', fileperms ($file)). Somefile.txt';
echo $filename.' filesize ($filename). 'bytes';
?.gt; [/code]
the entire information of a file has a function to return an array.