<title>php– File Operations Miscellaneous</title> php– File Operations Miscellaneous Table of Contents
- Create a file
- Generate a unique name
- mkdir Creating a Directory
- Traverse Directory
Create a file
Functions used: basename, file_exists, Touch
function CreateFile($filename) {// Verify that the file name is legal $pattern="/[\/, \*, <>, \?, \|] /";// * Add no ' \ ' does not affect, because [] inside only-and ^ have special meaning if(Preg_match ($pattern, BaseName ($filename))) {return ' Illegal file name '; }Else{// do you have a file with the same name if(File_exists ($filename)) {return ' file exists '; }Else{if(Touch ($filename)) {// chmod o+w Write permission to directory return ' Create success '; }Else{return ' Create failed '; } } }}
Generate a unique name
$uniName = MD5 (Uniqid (Microtime (True), true));
mkdir Creating a Directory
$ Path = dirname (__file__). '/upload/'; if (!file_exists ($path)) { mkdir ($pathtrue); True indicates that the meaning of mkdir-p can be created recursively chmod ($path, 0777); Although the above set permissions, but Umask has affected }
Traverse Directory
Opendir, Readdir, Is_file, Is_dir, Closedir
function readdirectory($Path) {if($Path[Strlen ($Path)-1] = ='/') {// remove the last slash $Path= substr ($Path, 0,-1); }$Handle= Opendir ($Path)or die(' could not open file '); while(($Item= Readdir ($Handle)) !==false) {if($Item=="."||$Item=="..") {Continue; }if(Is_file ($Path.'/'.$Item)) {// Is_file $arr[' file '][] =$Item; }if(Is_dir ($Path.'/'.$Item)) {// Is_dir $arr[' dir '][] =$Item; }} closedir ($Handle);return $arr;}
scandir
Returns the file in the current directoryls
$ files1 = Scandir ($dir); $ files2 = Scandir ($dir, 1);
Array ( [0] = =. [1] = = . [2] = = bar.php [3] = Foo.txt [4] = Somedir) Array ( [0] = Somedir [1] = Foo.txt
[2] = bar.php [3] = =. [4] = =.)
Glob, only the current directory is listed with the wildcard character
foreach (Glob ("*.txt"as$filename) { echo$filename ' <br> ';}
Funclist.txt
Funcsummary.txt
Quickref.txt
PHP--File operations Miscellaneous