PHP file operation function function Daquan
<?php/* Convert byte size */function transbyte ($size) {$arr =array ("B", "KB", "MB", "GB", "TB", "EB"), $i =0;while ($size >=1024) {$size/=1024; $i + +;} Return round ($size, 2). $arr [$i];} /* Detect if file name is legitimate */function Checkfilename ($filename) {$pattern = "/[\/,\*,<>,\?\|] /"; if (Preg_match ($pattern, $filename)) {return false;} Else{return true;}} /* Create file */function createFile ($filename) {if (Checkfilename ($filename)) {//detects if there is a file with the same name under the current directory,!file_exists ($filename ) {//through Touch ($filename) to create if (Touch ($filename)) {$mes = "file creation succeeded";} else{$mes = "File creation failed";}} else{$mes = "File already exists, please rename and create";}} else{$mes = "illegal file name";} return $mes;} /* Rename file */function renamefile ($oldname, $newname) {//Verify the legitimacy of the file name if (Checkfilename ($newname)) {//detects if a file with the same name exists in the current directory $path= DirName ($oldname); if (!file_exists ($path. " /". $newname)") {//To rename if (rename ($oldname, $path. ") /". $newname)" {$mes = "rename succeeded";} else{$mes = "rename Failed";}} else{$mes = "There is a file with the same name, please rename";}} Else{return $mes = "illegal file name";} return $mes;} /* Delete file */function delfile ($filename) {if (unlink ($filename)) {$mes = "file deletion succeeded";} else{$mes = "File deletion failed";} return $mes;}/* Download the file Operation */function Downfile ($filename) {header ("Content-disposition:attachment;filename=". BaseName ($filename)); Header ("Content-length:". FileSize ($filename)); ReadFile ($filename);} /* Copy the file */function copyFile ($filename, $dstname) {if (file_exists ($dstname)) {if (!file_exists ($dstname. ") /". BaseName ($filename))) {if (copy ($filename, $dstname.) /". BaseName ($filename))) {$mes =" file copy succeeded ";} else{$mes = "file copy Failed";}} else{$mes = "file with the same name exists";}} else{$mes = "Destination directory does not exist";} return $mes;} /* Cut the file */function cutfile ($filename, $dstname) {if (file_exists ($dstname)) {if (!file_exists ($dstname. ") /". BaseName ($filename))) {if (rename ($filename, $dstname.") /". BaseName ($filename))) {$mes =" file clipping succeeded ";} else{$mes = "file clipping failed";}} else{$mes = "file with the same name exists";}} else{$mes = "Destination directory does not exist";} return $mes;} /* Upload file */function uploadfile ($fileInfo, $path, $allowExt =array ("gif", "JPEG", "JPG", "png", "TXT"), $maxSize =10485760) { Determine if the error number if ($fileInfo [' Error ']==upload_err_ok) {//file is uploaded via HTTP post (Is_uploaded_file ($fileInfo [' Tmp_name ') ) {//upload file name, only allow uploading of jpeg|jpg, PNG, GIF, TXT files//$ALLOWEXT =arraY ("gif", "JPEG", "JPG", "png", "TXT"), $ext =getext ($fileInfo [' name ']), $uniqid =getuniqidname (); $destination = $path. " /". PathInfo ($fileInfo [' Name '],pathinfo_filename)." _ ". $uniqid.". $ext, if (In_array ($ext, $allowExt)) {if ($fileInfo [' Size ']<= $maxSize) {if (Move_uploaded_file ($fileInfo [' Tmp_name '], $destination)) {$mes = "file upload succeeded";} else{$mes = "file move Failed";}} else{$mes = "file too large";}} else{$mes = "illegal file type";}} else{$mes = "File not uploaded by HTTP POST";} Else{switch ($fileInfo [' ERROR ']) {Case 1: $mes = "exceeds the size of the profile"; Break;case 2: $mes = "exceeds the size of the form allowed to receive data"; Break;case 3: $mes = " File part is uploaded "; Break;case 4: $mes =" no file uploaded "; break;}} return $mes;}? >
PHP file operation function function Daquan