PHP5 classic tutorial file operation_PHP tutorial
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.
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"; |
$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; |
$file = dirlist.php; |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

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.

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.

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:

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

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.

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
