Home Backend Development PHP Tutorial PHP5 classic tutorial file operation_PHP tutorial

PHP5 classic tutorial file operation_PHP tutorial

Jul 13, 2016 pm 05:39 PM
php5 one exist object operate Tutorial document yes classic computer equipment

1. Introduction

In any computer device, files are necessary objects. In web programming, file operations have always been a headache for web programmers. However, file operations are necessary in cms systems. Very useful. We often encounter operations such as generating file directories and editing files (folders). Now I will make a detailed summary of these functions in PHP and demonstrate how to use them with examples. For a detailed introduction to the corresponding functions, please refer to the PHP manual. .Here we only summarize the key points and points that need attention. (This is not found in the PHP manual.)

2. Directory operations

The first introduction is a function that reads from a directory, opendir(), readdir(), closedir(). When used, the file handle is opened first, and then iteratively listed:

$base_dir = "filelist/";
$fso = opendir($base_dir);
echo $base_dir."
" ;
while($flist=readdir($fso)){
echo $flist."
" ;
}
closedir($fso)
?>

This is a program that returns the files and directories under the file directory (0 files will return false).

Sometimes you need to know directory information. You can use dirname($path) and basename($path) to return the directory part and file name part of the path respectively. You can use disk_free_space($path) to return the free space.

Create command:

mkdir($path,0777)

, 0777 is the permission code, which can be set by the umask() function under non-window conditions.

rmdir($path)

The file with the path in $path will be deleted.

dir -- directory class is also an important class for operating file directories. It has three methods, read, rewind, and close. This is a pseudo-object-oriented class. It first uses open file handles, and then uses pointers. Read., see the php manual here:

$d = dir("/etc/php5");
echo "Handle: " . $d->handle . " ";
echo "Path: " . $d->path . " ";
while (false !== ($entry = $d->read())) {
    echo $entry." ";
}
$d->close();
?>

Output:

Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli

File attributes are also very important. File attributes include creation time, last modification time, owner, file group, type, size, etc.

Let’s focus on file operations.

3. File Operation

A. Read file

First, check whether a file can be read (permission issue), or whether it exists. We can use the is_readable function to obtain the information.

$file = dirlist.php;
if (is_readable($file) == false) {
die(文件不存在或者无法读取);
} else {
echo 存在;
}
?>

$file = dirlist.php;
if (is_readable($file) == false) {
                                        die (the file does not exist or cannot be read);
} else {
echo exists;
}
?>

The function to determine the existence of a file also includes file_exists (demonstrated below), but this is obviously not as comprehensive as is_readable. When a file exists, you can use

$file = "filelist.php";
if (file_exists($file) == false) {
die(文件不存在);
}
$data = file_get_contents($file);
echo htmlentities($data);
?>

$file = "filelist.php";
if (file_exists($file) == false) {
                                                                                              die (the file does not exist);
}
$data = file_get_contents($file);
echo htmlentities($data);
?>

However, the file_get_contents function is not supported on lower versions. You can first create a handle to the file, and then use a pointer to read the entire file:

         $fso = fopen($cacheFile, r);
         $data = fread($fso, filesize($cacheFile));
         fclose($fso);

There is another way to read binary files:

$data = implode(, file($file));

B. Write file

Same as reading a file, first check if it can be written:

$file = dirlist.php;
if (is_writable($file) == false) {
die("我是鸡毛,我不能");
}
?>

$file = dirlist.php;
if (is_writable($file) == false) {
             die("I am a chicken feather, I can't");
}
?>

http://www.bkjia.com/PHPjc/486424.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486424.htmlTechArticle1. Introduction In any computer device, files are necessary objects, and in web programming, File operations have always been a headache for web programmers, and file operations in the cms system...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
2024 CSRankings National Computer Science Rankings Released! CMU dominates the list, MIT falls out of the top 5 2024 CSRankings National Computer Science Rankings Released! CMU dominates the list, MIT falls out of the top 5 Mar 25, 2024 pm 06:01 PM

The 2024CSRankings National Computer Science Major Rankings have just been released! This year, in the ranking of the best CS universities in the United States, Carnegie Mellon University (CMU) ranks among the best in the country and in the field of CS, while the University of Illinois at Urbana-Champaign (UIUC) has been ranked second for six consecutive years. Georgia Tech ranked third. Then, Stanford University, University of California at San Diego, University of Michigan, and University of Washington tied for fourth place in the world. It is worth noting that MIT's ranking fell and fell out of the top five. CSRankings is a global university ranking project in the field of computer science initiated by Professor Emery Berger of the School of Computer and Information Sciences at the University of Massachusetts Amherst. The ranking is based on objective

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

What should I do if there is no sound in the system after win11 update? How to solve the problem of no sound in win11 device What should I do if there is no sound in the system after win11 update? How to solve the problem of no sound in win11 device Jun 25, 2024 pm 05:19 PM

After some users have updated and upgraded the win11 system, the computer has no sound. The problem of loving you is usually caused by no device, missing sound card driver, or unknown error. So how should we solve these problems? , this issue of win11 tutorial is here to answer everyone’s questions. Next, let’s take a look at the detailed steps. Solution to no sound after win11 upgrade: 1. No device 1. If we are using a desktop computer, it is probably because there is no device. 2. Because ordinary desktop computers do not come with built-in speakers, we need to plug in speakers or headphones to have sound. 2. The sound card driver is missing 1. After we update the Win11 system, the original sound card or audio device driver may not be available.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

A simple tutorial on converting full-width English letters to half-width letters A simple tutorial on converting full-width English letters to half-width letters Mar 25, 2024 pm 09:21 PM

When using a computer to input English, sometimes we encounter the difference between full-width English letters and half-width English letters. Full-width English letters refer to the characters input by pressing the Shift key and the English letter key combination when the input method is Chinese mode. They occupy a full-width character width. Half-width English letters refer to characters input directly when the input method is English mode, and they occupy half a character width. In some cases, we may need to convert full-width English letters to half-width letters. Here is a simple tutorial: First, open a text editor or any

See all articles