PHP Surface Test File directory operation _php Instance

Source: Internet
Author: User
This problem is not difficult in fact, the test center is mainly in the function substr () STRRCHR () Array_pop () Strrpos () Strpos () Strrev () explode () PathInfo ().

Not much nonsense to say, directly to everyone put the code.

Get file suffix, traverse directory hierarchy

/*** 5 ways to get the file suffix name * This problem is actually the test function substr () STRRCHR () Array_pop () Strrpos () Strpos () Strrev () explode () pathinfo () * @param strin G $filename the file name to process 1.jpg/1.png/1.html/1.php* @return string. php/php */function Get_ext1 ($filename) {return STRRCHR ($f Ilename, '. ');} function Get_ext2 ($filename) {return substr ($filename, Strrpos ($filename, '. '));} function Get_ext3 ($filename) {return Array_pop (Explode ('. ', $filename));} function Get_ext4 ($filename) {return pathinfo ($filename, pathinfo_extension);} function Get_ext5 ($filename) {return Strrev (substr (Strrev ($filename), 0,strpos (Strrev ($filename), '. ')));} $filename = "1.php"; Echo get_ext1 ($filename). " \ n "; Echo get_ext2 ($filename)." \ n "; Echo get_ext3 ($filename)." \ n "; Echo Get_ext4 ($filename)." \ n "; Echo get_ext5 ($filename)." \ n ";/*** traverse Directory (Recursive) * @param string $dir directory name and finally no/* @return array $files Entire directory structure */function myscandir ($dir) {$files = array (    ); if ($handle = Opendir ($dir)) {while (($file = Readdir ($handle)) = False) {if ($file! = ". "&& $file! =" ... ") {$newDir = $dir. "                /". $file;                if (Is_dir ($newDir)) {$files [$file] = Myscandir ($newDir);                }else{$files [] = $file;        }}} closedir ($handle);    return $files; }} $dir = "/dirname";p Rint_r (Myscandir ($dir));

The above code is not very simple ah, on these functions, functions mastered, the problem is easy to solve, I hope this article to share to help you.

  • 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.