Directory Operations
$base _dir = ' filelist/'; $fso = Opendir ($base _dir);//Open Directory, return directory handle echo ' Basedir: '. $base _dir. '
'; while ($filelist = Readdir ($FSO)) {echo $filelist. '
';} Closedir ($FS);//close directory handle
You can use DirName ($path) and basename ($path) to return the directory part and file name portion of the path separately, and you can return the remaining size of the space with Disk_free_space ($path).
To create a command:
mkdir ($path, 0777): 0777 is the permission code that is available under non-Windows Umask () to set
RmDir ($path): Delete file under Path $path
New file
first, to determine the permissions of the directory in which the file was created, the recommended setting is 777, and the name of the new file suggests using absolute paths .
$filename = ' Test.txt '; $fp = fopen ($filename, ' w+ ');//Open file pointer, create file if (!is_writable ($filename)) {die (' file: '. $ filename. ' Not writable, please check! ');} Fclose ($FP);//Close pointer
Read File
First, determine whether the file can be read (permissions issues), and then determine if the file exists
$filename = ' test.txt '; if (is_readable ($filename) = = False) {die (' file does not exist or cannot be read ');} if (file_exists ($filename) = = False) {die (' file does not exist ');} $content = file_get_contents ($filename); Echo htmlentities ($data);
Write a file
$filename = ' test.txt '; if (is_writeable ($filename) = = False) {Die (' cannot write ');} $content = ' Hello world '; file_put_contents ($filename, $content);
File_put_contents () is a newly introduced function in PHP5, the lower version of PHP is not available and can be used in the following ways:
$filename = ' Test.txt '; $data = ' Hello world '; $handle = fopen ($filename, ' W '); fwrite ($handle, $data); fclose ($handle);
when writing to a file, you sometimes need to lock and write
function Cache_page ($pageurl, $pagedata) {if (! $fso = fopen ($pageurl, ' W ')) {$this->warns (' cannot open cache file '); return false;} LOCK_NB, exclusive lock if (!flock ($fsom, lock_ex)) {$this->warns (' cannot lock cache file '); return false;} Write byte stream, serizlize write to other format if (!fwrite ($fso, $pagedata)) {$this->warns (' cannot write to cache file '); return false;} Release lock Flock ($FSO, Lock_un); fclose ($FSO); return true;}
deleting files
$file = ' Test.txt '; $result = @unlink ($file);
Copying files
$file = ' Test.txt '; $newfile = ' new.txt ';//This file parent folder must be writable if (file_exists ($file) = = False) {die (' file does not exist ');} $result = Copy ($file, $newfile), if ($result = = False) {echo ' Done ';}
Get file Properties
$file = ' test.txt ';//Gets the last modified time of the Echo date (' R ', Filemtime ($file));//Gets the last accessed time echo date (' R ', Fileatime ($file));// Returns the file owner Echo posix_getpwuid (Fileowner ($file));//Get file permissions, non-Winecho substr (sprintf (' $o ', fileperms ($file),-4));// Returns the file size echo filesize ($file). ' Bytes ';//Return all information about the file Var_dump (stat ($file));
The above describes the PHP file and directory operations, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.