PHP Directory Operation example Summary _php skill

Source: Internet
Author: User
Tags php programming

This article summarizes the PHP directory operation method. Share to everyone for your reference, specific as follows:

Directory Operations

new directory : mkdir (path, permissions, recursive creation)

Delete directory : RmDir ()

Mobile (renamed): rename ()

Get directory Contents :

Open Directory

Directory handle = Opendir ()

Reading directories

filename = readdir (directory handle)

Read the file name in turn, move the handle pointer down, and return false when it is not read

Close Directory

Closedir ()

Recursively read Directory contents:

<?php
Showdir ('.. /.. /file ');
function Showdir ($path, $dep =0) {
 $pos = Opendir ($path);
 while (false!== $file =readdir ($pos)) {
  if ($file = = '. ') $file = = '.. ') Continue;
  echo str_repeat ("", $dep *4), $file. ' </br> ';
  if (Is_dir ($path. ') /'. $file)) {
   $func = __function__;
   $func ($path. ' /'. $file, $DEP + 1);}}


The Operation effect chart is as follows:

<?php
$res = Showdir ('.. /.. /file ');
Echo ' <pre> ';
Print_r ($res);
function Showdir ($path) {
 $pos = Opendir ($path);
 $next = Array ();
 while (false!== $file =readdir ($pos)) {
  if ($file = = '. ') $file = = '.. ') Continue;
  $fileinfo = Array ();
  $fileinfo [' name '] = $file;
  if (Is_dir ($path. ') /'. $file)) {
   $fileinfo [' type '] = ' dir ';
   $func = __function__;
   $fileinfo [' next '] = $func ($path. ') /'. $file);
  } else{
   $fileinfo [' type '] = ' file ';
  }
  $next [] = $fileinfo;
 }
 Closedir ($pos);
 return $next;
}

The Operation effect chart is as follows:

To delete a directory recursively:

<?php
Showdir ('.. /.. /file/sim ');
function Showdir ($path, $dep =0) {
 $pos = Opendir ($path);
 while (false!== $file =readdir ($pos)) {
  if ($file = = '. ') $file = = '.. ') Continue;  Echo Str_repeat ("", $dep *4), $file. ' </br> ';
  if (Is_dir ($path. ') /'. $file)) {
   $func = __function__;
   $func ($path. ' /'. $file, $DEP + 1);
  } else{
   unlink ($path. ' /'. $file);
  }
 RmDir ($path);
 Closedir ($pos);


Directory file Encoding problem:

When presented, converts the operating system code into a response data encoding

Windows for GBK, Project Utf-8

Iconv (' GBK ', Utf-8 ', file);

Code address in Chinese: need to convert to system encoding

Iconv (Utf-8 ', ' GBK ', file);

More about PHP Interested readers can view the site topics: "PHP Directory Operation tips Summary", "PHP file Operation Summary", "PHP Array" operation Skills Encyclopedia, "PHP Basic Grammar Introductory Course", "PHP operation and operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with your 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.