PHP some well-packaged functions for files

Source: Internet
Author: User
Tags http post

<?PHP//Bytes/kb/mb/gb/tb/eb/** * Convert byte size * @param number $size * @return number*/functionTransbyte ($size) {    $arr=Array("B", "KB", "MB", "GB", "TB", "EB" ); $i= 0;  while($size>= 1024 ) {        $size/= 1024; $i++; }    return round($size, 2).$arr[$i];}/** * Create file * @param string $filename * @return String*/functionCreateFile ($filename) {    //file/1.txt//Verify the legality of the file name, whether it contains/,*,<>,?, |    $pattern= "/[\/,\*,<>,\?\|] /"; if(!Preg_match($pattern,basename($filename ) )) {        //detects if a file with the same name exists in the current directory        if(!file_exists($filename )) {            //using Touch ($filename) to create            if(Touch($filename )) {                return"File creation succeeded"; } Else {                return"File creation failed"; }        } Else {            return"File already exists, please rename and create"; }    } Else {        return"Illegal file name"; }}/** * Rename file * @param string $oldname * @param string $newname * @return String*/functionRenameFile ($oldname,$newname){//echo $oldname, $newname;//Verify file name is legal    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)){                return"Rename succeeded"; }Else{                return"Renaming failed"; }        }Else{            return"There is a file with the same name, please rename"; }    }Else{        return"Illegal file name"; }    }/** * Check if file name is valid * @param string $filename * @return Boolean*/functionCheckfilename ($filename){    $pattern= "/[\/,\*,<>,\?\|] /"; if(Preg_match($pattern,$filename )) {        return false; }Else{        return true; }}/** * Delete file * @param string $filename * @return String*/functionDelfile ($filename){    if(unlink($filename)){        $mes= "File deletion succeeded"; }Else{        $mes= "File deletion failed"; }    return $mes;}/** * Download file operation * @param string $filename*/functionDownfile ($filename){    Header("Content-disposition:attachment;filename=".basename($filename)); Header("Content-length:".)filesize($filename)); ReadFile($filename);}/** * Copy file * @param string $filename * @param string $dstname * @return String*/functionCopyFile ($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;}functionCutfile ($filename,$dstname){    if(file_exists($dstname)){        if(!file_exists($dstname." /".basename($filename))){            if(Rename($filename,$dstname." /".basename($filename))){                $mes= "File Cut succeeded"; }Else{                $mes= "File cut Failed"; }        }Else{            $mes= "file with the same name exists"; }    }Else{        $mes= "Destination directory does not exist"; }    return $mes;}/** * Upload file * @param array $fileInfo * @param string $path * @param array $allowExt * @param int $maxSize * @return Strin G*/functionUploadFile ($fileInfo,$path,$allowExt=Array("GIF", "JPEG", "JPG", "png", "TXT"),$maxSize=10485760){    //Judging the error number    if($fileInfo[' Error ']==UPLOAD_ERR_OK) {        //whether the file was uploaded via the HTTP POST method        if(Is_uploaded_file($fileInfo[' Tmp_name '])){            //upload file name, only allow upload jpeg|jpg, PNG, GIF, TXT file//$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 is not uploaded via HTTP POST"; }    }Else{        Switch($fileInfo[' Error ']){             Case1:$mes= "The size of the configuration file is exceeded";  Break;  Case2:$mes= "exceeds the size of the form allowed to receive data";  Break;  Case3:$mes= "File part is uploaded";  Break;  Case4:$mes= "No files are uploaded";  Break; }    }        return $mes; }

PHP some well-packaged functions for files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.