[PHP] PHP creates the specified directories and files

Source: Internet
Author: User
Tags create directory

A few days ago to see someone asked how to create a file in the PHP environment under the specified directory, just their recent study, after a test, finally the results, posted out to share with you.
Directory structure:
The file where the code is located wwwroot/mydir/test/test.php
Create directory: Create a directory testjiang123 under wwwroot/mydir/.
Create file: Create a file test.html under wwwroot/mydir/testjiang123/.
  1. Create a folder
  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. Create a file

  14. function Creat_file ($path) {
  15. $sFile = "test.html";
  16. $var _content= "File contents";

  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. A PHP copy function is also added here, copy the mytest.php in the current directory to the directory you just created, and rename it to the directory name +_mytest.php
  28. copy ("mytest.php", $dir. " /". $username." _mytest.php ");

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.