首页 新随笔 联系 订阅 管理 个人网站

[php] PHP创建指定目录和文件

前几天看到有人问PHP环境下如何创建文件到指定目录下,正好自己最近在学习,经过一翻测试,终于出结果了,贴出来与大家分享。
目录结构:
代码所在的文件wwwroot/mydir/test/test.php
创建目录:在wwwroot/mydir/下创建目录testjiang123。
创建文件:在wwwroot/mydir/testjiang123/下创建文件test.html。

  1. //创建文件夹
  2. $username="testjiang123";
  3. $dir="../mydir/" . $username;
  4. function createdir($dir)
  5. {
  6.    if(file_exists($dir) && is_dir($dir)){
  7.    }
  8.    else{
  9.     mkdir ($dir,0777);
  10.    }
  11.    creat_file($dir);
  12. }
  13. //创建文件

  14. function creat_file($path){
  15.     $sFile = "test.html";
  16.     $var_content="文件内容";

  17.    if (file_exists($path."/".$sFile)) {
  18.     creat_file();
  19.    } else {
  20.     $fp= fopen($path."/".$sFile,"w");
  21.     fwrite($fp,$var_content);
  22.     fclose($fp);
  23.    }
  24.    return $sFile;
  25. }

  26. createdir($dir);
  27. //此处还加了一个PHP的COPY函数,COPY当前目录下的mytest.php到刚才创建的目录下,并重新命名为目录名+_mytest.php
  28. copy("mytest.php",$dir."/".$username."_mytest.php");
posted @ 2014-06-03 14:31  __不粘锅  阅读(3388)  评论(0编辑  收藏  举报