Php operations on files,

Source: Internet
Author: User

Php operations on files,

How can I display folders and directories on my disk on a webpage? Let's take a look at how php is used to operate on them.

Php files generally contain two pieces of content. The files and directories should first look at the code in one sentence and their functions.

View the result after running

File indicates the meaning of a file.

Change the path again

After running

The dir here is short for the Directory

In fact, there are two types of files: files or directories.

The paths written here are all files and directories created in the current directory,

Wenjiancaozuo. php is the name of the file currently being written.

The three qq.txt 111.php files are all in the same directory. If you want to write them, you can name them as needed.

Next let's continue.

The code above indicates that dir is short for a directory. After judging that the file type 111.php is a directory, run this code to see what content it will return.

Next, let's look at the values returned by non-directories.

If you replace dir with file in the code above, it can also be used to determine whether the file is a file. If yes, true is returned. If no, false is returned.

Next, let's talk about file attributes.

What is file attributes? Take a look at the screenshot.

Such a file is called a file attribute.

Let's take a look at the code.

Still need a file path

It uses qq.txt.

Check the running result.

The result is a timestamp.

Convert it to something you can understand.

Use the date Method

Check the running result.

The time displayed in the file is the same.

Let's take a look.

It is a letter different from the previous sentence. The running result is the same as the last access time.

This is also a different letter, and the result is the same as the above two time run results

Let's take a look at the running result.

There is no content in the file, so the file size is 0

Enter some content in it, and then check the running result.

Then run the code. The result is:

This file exists. Run it to see what will be returned.

Write a non-existent file path to see what value will be returned.

After running

Let's talk about the file path.

Run

The root path of the server is displayed.

This is a file from the parent directory.

After running

The file name is displayed.

You can also use basename to write two parameters.

Check the running result.

Check the running result.

There are only two points except the file name.

After running

Returns an array.

Check the running result.

 

 

Check the running result.

The returned results may vary according to the parameters in the brackets.

For example:

 

In this example, the suffix is. txt.

In this way, the file is suffixed with. php.

You can use the above Traversal method to traverse all the files in the 111. php Directory.

Check the running result.

Let's take a look at the running results.

Verify and see if result 5 is correct.

3 + 2: 5

Let's take a look at the running result.

All files in the current directory

If you want to get the file name with a path, you can write it like this

Check the running result.

There is a... here you should pay attention

. Indicates the current directory.. indicates the parent directory.

For general operations, remove the two.

// Obtain the size of all files in a directory. function Fsize ($ fname) {$ size = 0; $ dir = opendir ($ fname ); while ($ u = readdir ($ dir) {if ($ u = ". "| $ u = ".. ") {} else {$ zfname = $ fname. "/". $ u; if (is_file ($ zfname) {$ size + = filesize ($ zfname) ;}}// locate the subdirectory, obtain the file size in the subdirectory rewinddir ($ dir); while ($ u = readdir ($ dir) {if ($ u = ". "| $ u = ".. ") {} else {$ zfname = $ fname. "/". $ u; if (is_dir ($ zfname) {$ size + = Fsize ($ zfname) ;}} closedir ($ dir); return $ size ;} echo Fsize (". /111.php ");

This is the file size.

To run

Verify

Find another file with content

Change the path to this and check the running result.

To verify

 

Creating a folder or deleting a folder is not only a method of right-clicking, but can also be operated by code.

First, let's look at the files in the directory before the code is run.

Click RUN code to see if there is a folder named aa added

Added

Let's see how to delete

After you click Run, the folder named aa is deleted.

Of course, you should note that only empty folders can be deleted by the code, and folders with content cannot be deleted.

The preceding path is an old path, followed by a new path and a new name is given to it.

Check the files before running.

After the code is run, the folder does not exist in the original directory.

Let's look at its parent directory.

Tupian. php

 

After running

Created successfully

The old path and the new path

After running the command, check whether the file has been copied successfully.

 

Running successful

After running, check whether the file is available.

The file named 22.txt has been deleted.

Write some content in the 22 documents in the current directory, and then save

Then the Code Section

After running the command, check whether the content can be output.

Output successfully on the webpage

This method not only obtains local content, but also remote content, and connects Baidu to create a column.

After running

Get Baidu's page

The preceding parameter is the file path, followed by the content to be added.

After running the command, check whether the file contains any added content.

 

Added

Check the running result.

Check the running result.

The previous content is missing, but the content is re-entered.

Change w to

 

Run

An additional sentence is added without rewriting the content.

 

 

Run

 

The first letter in the file

Copy more sentences

After running

This method can read the content and read one character each time.

Add the content in the file before running, and then save

Run code

 

The first row is read.

Write another sentence and run

The contents of the first and second rows are read.

Check the running result.

The output is three strings.

Users can only read and write system files, such as. txt. php. They can only operate on the server and cannot operate on the client.

The above describes how to delete folders, but only folders with no content can be deleted. How can we delete files with content?

Delete this folder.

 

 

Run the command and check whether the folder exists in the directory.

 

Deleted

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.