Php file operation summary (delete a specified file/get the file name in the folder/read the name of the folder as shown in the following figure)

Source: Internet
Author: User
This article mainly introduces php file operations, including deleting a specified file, obtaining the file name under a folder, and reading the folder name. it involves common php operations on files and directories, for more information about php file operations, see the examples in this article. We will share this with you for your reference. The details are as follows:

1. delete an object

Unlink ()

Syntax: int unlink (string filename );

Return value: integer

Function type: file access. For example:

unlink("tmp/test.txt");

2. get the file name under the folder

$ Dir = "message/"; // folder name if (is_dir ($ dir) {if ($ dh = opendir ($ dir )) {while ($ file = readdir ($ dh ))! = False) {echo "file name: $ file
";}Closedir ($ dh );}}

3. read the image name under the folder

<? Php $ handle = opendir ('images/'); // the current directory while (false! ==( $ File = readdir ($ handle) {// traverse the list ($ filesname, $ kzm) of the directory where the php file is located = explode (". ", $ file); // Obtain the extension if ($ kzm =" gif "or $ kzm =" jpg "or $ kzm =" JPG ") {// File Filter if (! Is_dir ('. /'. $ file) {// folder filter $ array [] = $ file; // Save the qualified file name to the array $ I ++; // record the total number of images }}print_r ($ array);?>

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.