PHP Create/delete/copy folders, file _php tips

Source: Internet
Author: User
Tags create directory fread learn php mkdir

Learn PHP's file programming, in which PHP itself provides a copy of the copied file function (copy). I also wrote a function similar to the copy of the picture function, as a record here.
Before you say this function, introduce the knowledge of creating/deleting folders, files, and so on using PHP.
1. Create a folder

<?php 
 //Use File_exists ("D:/mydir") or Is_dir ("D:/mydir") to determine if the folder exists 
 if (!file_exists ("D:/mydir")) { 
  if (MkDir ("D:/mydir")) { 
   echo "Create folder succeeded"; 
  } else{ 
   echo "Create folder Failed"; 
  } 
 } else { 
  echo "This folder already exists"; 
 } 

2. Create a multilevel directory (above, you can only create a level of directory)

<?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 "Create folder succeeded"; 
  } else{ 
   echo "Create folder Failed"; 
  } 
 } else { 
  echo "This folder already exists"; 
 } 
? > 

3. Delete Folder

<?php 
 //If there are files or directories under the folder, you cannot delete the success 
 if (RmDir ("D:/mydir/p/h/p/test")) { 
  echo Delete folder succeeded; 
 } else { 
  echo "Delete folder succeeded"; 
 } 

4. Creation of documents

<?php 
 //In D:/mydir directory, create a file and write Hello 
 $file _path= "D:/mydir2/test.txt"; 
 $FP =fopen ($file _path, "w+"); 
  
 Fwrite ($fp, "Hello,world"); 
 Fclose ($FP); 
 echo "File write succeeded"; 
? >

5. Delete Files

<?php 
 $file _path= "D:/mydir2/test.txt"; 
 if (Is_file ($file _path)) { 
  if (unlink ($file _path)) { 
   echo "delete succeeded"; 
  } else { 
   echo "delete failed"; 
  } 
 } else { 
  echo "file does not exist"; 
 } 

Functions for copying files:

 <?php//php itself provides copy file functions: Apply Copy Picture//copy ("source", "place")//$file _path=iconv ("Utf-8", "GB 
 2312 "," including Chinese path "); Converts the UTF-8 encoding into gb2312 code/*if (!copy ("C:\\bh"). 
 PNG "," D:\\bh2.png ")) {echo ' error '; 
 else {echo ' OK '; 
    
   }*///self-made Copy file functions function Mycopyfunc ($res, $des) {if (file_exists ($res)) {$r _fp=fopen ($res, "R"); 
   Positioning $pos =strripos ($des, "\ \"); 
   $dir =substr ($des, 0, $pos); 
    if (!file_exists ($dir)) {//can create multilevel directory mkdir ($dir, 0777,true); 
   echo "Create directory success <br/>"; 
   $d _fp=fopen ($des, "w+"); 
 
   $fres =fread ($r _fp,filesize ($res)); 
   Side-read side write $buffer = 1024; 
   $fres = ""; 
    while (!feof ($r _fp)) {$fres =fread ($r _fp, $buffer); 
   Fwrite ($d _fp, $fres); 
   } fclose ($r _fp); 
 
   Fclose ($d _fp); 
  echo "Copy succeeded"; 
  else {echo ' source file does not exist '; } mycopyfunc ("C:\\bh. PNG "," D:\\PHPTEST\\TEST\BH2. 
 PNG "); Mycopyfunc ("C:\\BH.") PNG "," D:\\BH. 
PNG "); ?> 

The above code contains the file reading and writing functions in PHP file programming, and I hope you can learn PHP program design to help.

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.