PHP creates/deletes/copies folders and files

Source: Internet
Author: User
This article describes how to use PHP to create, delete, and copy folders and files. if you are interested, you can refer to PHP file programming, PHP itself provides the copy function ). I also wrote a function for copying images with similar functions, so that I can record it here.
Before talking about this function, let's first introduce how to use PHP to create/delete folders and files.
1. create a folder

<? Php // use file_exists ("d:/mydir") or is_dir ("d:/mydir") to determine whether the folder exists if (! File_exists ("d:/mydir") {if (mkdir ("d:/mydir") {echo "folder created successfully ";} else {echo "failed to create folder" ;}} else {echo "this folder already exists" ;}?>

2. create a multi-level Directory (as mentioned above, only a level-1 directory can be created)

<? Php // use file_exists ("d:/mydir") or is_dir ("d:/mydir") to determine whether the folder exists $ path = "d: /mydir/p/h/p/test "; if (! File_exists ($ path) {if (mkdir ($ path, 0777, true) {echo "folder created successfully" ;}else {echo "folder created failed ";}} else {echo "this folder already exists" ;}?>

3. delete a folder

<? Php // if there are files or directories in the folder, they cannot be deleted successfully if (rmdir ("d:/mydir/p/h/p/test ")) {echo "folder deleted";} else {echo "folder deleted" ;}?>

4. file creation

<? Php // in the d:/mydir Directory, create a file and write it to hello $ file_path = "d:/mydir2/test.txt"; $ fp = fopen ($ file_path, "w +"); fwrite ($ fp, "hello, world"); fclose ($ fp); echo "file written successfully";?>

5. delete an object

<? Php $ file_path = "d:/mydir2/test.txt"; if (is_file ($ file_path) {if (unlink ($ file_path) {echo "deleted successfully ";} else {echo "failed to delete" ;}} else {echo "file does not exist" ;}?>

File copying function:

<? Php // The copy File function provided by PHP itself: the app copies images // copy ("source", "location") // $ file_path = iconv ("UTF-8 ", "gb2312", "path containing Chinese characters"); // convert UTF-8 encoding into gb2312 Code/* if (! Copy ("C: \ bh. PNG "," D :\\ bh2.png ") {echo 'error';} else {echo 'OK ';} * // function myCopyFunc ($ res, $ des) {if (file_exists ($ res) {$ r_fp = fopen ($ res, "r"); // locate $ pos = strripos ($ des, "\"); $ dir = substr ($ des, 0, $ pos); if (! File_exists ($ dir) {// multi-level directory mkdir ($ dir, 0777, true) can be created; echo "directory created successfully
";}$ D_fp = fopen ($ des," w + "); // $ fres = fread ($ r_fp, filesize ($ res )); // read and write $ buffer = 1024; $ fres = ""; while (! Feof ($ r_fp) {$ fres = fread ($ r_fp, $ buffer); fwrite ($ d_fp, $ fres);} fclose ($ r_fp ); fclose ($ d_fp); echo "copied successfully";} else {echo "the source file does not exist";} myCopyFunc ("C: \ bh. PNG "," D :\\ PHPTest \ test \ bh2.PNG "); // myCopyFunc (" C: \ bh. PNG "," D: \ bh. PNG ");?>

The code above contains the file reading and writing functions in php file programming. I hope it will help you to learn about php programming.

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.