老爷子88 发表于 2017-3-27 12:49:33

php基本文件操作````

  纯静态站快做好了,也学到了好多.下面将关于php文件操作的一些简单方法和
  函数和大家分享一下:
<? //文件上传 function uploadFile($file,$isImage=false){ set_time_limit(0);$file_name=$file['name'];$file_type=$file['type'];$file_tmpname=$file['tmp_name'];$file_size=$file['size'];$file_error=$file['error'];if(!empty($file_name)){   if($isImage==true){    if ($file_error==UPLOAD_ERROR_OK) {   if ($file_type=="image/gif"||$file_type=="image/jpg"||$file_type=="image/pjpeg"||$file_type=="image/jpeg"||$file_type=="image/x-png")   {      $upload_path="../upload/pic/";      $upload_name=strrchr($file_name,".");      $upload_name=date("YmdHiss").$upload_name;      $upload_path=$upload_path.$upload_name;      if(move_uploaded_file($file_tmpname,$upload_path)){       return $upload_path;      }else {             echo "<script>alert('上传失败');history.back();</script>";      }   }else {      echo "<script>alert('图片格式不对');history.back();</script>";   }    }else {   echo "<script>alert('".$file_error."');history.back();</script>";    }   }else {    if ($file_error==UPLOAD_ERROR_OK) {   $upload_path="../upload/down/";   $upload_name=strrchr($file_name,".");   $upload_name=date("YmdHis").$upload_name;       $upload_path.=$upload_name;   if(move_uploaded_file($file_tmpname,$upload_path)){      return $upload_path;   }else {      echo "<script>alert('上传失败');history.back();</script>";   }    }      }}else {   if($isImage==true){    echo "<script>alert('请选择你要上传的图片!');history.back();</script>";   }else {    echo "<script>alert('请选择你要上传的软件!');history.back();</script>";   }   }}//创建文件夹 function createFolder($path) {    if (!file_exists($path))    {   createFolder(dirname($path));mkdir($path, 0777);    } } //删除文件夹和文件夹下的所有文件 function deldir($dir) {   $dh=opendir($dir);   while ($file=readdir($dh)) {   if($file!="." && $file!="..") {       $fullpath=$dir."/".$file;       if(!is_dir($fullpath)) {         unlink($fullpath);       } else {         deldir($fullpath);       }   }   }closedir($dh);   if(rmdir($dir)) {   return true;   } else {   return false;   } }//删除指定路径的文件function dir_delete($file)      {         if(file_exists($file))         {             if(is_dir($file))             {               $handle =opendir($file);               while(false!==($filename=readdir($handle)))               {                     if($filename!="."&&$filename!="..")$this->dir_delete($file."/".$filename);               }               closedir($handle);               rmdir($file);               return true;             }             else             {               unlink($file);             }         }   }//dir_delete('news/6061.htm');/*** 删除指定目录下的所有文件** @param String $dir要进行操作的路径* 适合范围,只有用于文件夹内不存在子文件夹的情况下* 来源DZ* 小佳(www.phpcina.cn)整理 于 2006-06-26   */ function dir_clear($dir) {   $directory = dir($dir);                //创建一个dir类(PHP手册上这么说的),用来读取目录中的每一个文件   while($entry = $directory->read()) {   //循环每一个文件,并取得文件名$entry         $filename = $dir.'/'.$entry;       //取得完整的文件名,带路径的         if(is_file($filename)) {         //如果是文件,则执行删除操作             @unlink($filename);         }   }   $directory->close();                   //关闭读取目录文件的类   return true; }?>
页: [1]
查看完整版本: php基本文件操作````