Home Backend Development PHP Tutorial How to operate php files

How to operate php files

Jul 05, 2018 pm 04:27 PM
php file operations

This article mainly introduces the method of operating php files, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

php File operations

1. Before learning PHP file operations, let us first understand these commonly used file operation functions.

1. is_dir() means: Determine whether the given file name is a directory

2. is_file() means: Determine whether the file exists

3. mkdir () means: create directory

4. rmdir() means: delete directory

5. unlink() means: delete file

6. fopen() It means: open the file

7. fwrite() means: write the file

8. fclose() means: close the open file

9. is_writable( ) means: Determine whether the given file is writable

10. is_readable() means: Determine whether the given file is readable.

11. File_get_contents() means: read the file and output it.

The map is as follows:




##First of all, let’s Create the test.php file and write the code. Then determine whether study is a directory. There are two situations in total, one situation: if it is not a directory, and the other situation if it is a directory.

Code:

if(!is_dir(‘study’))
{
    //如果不是目录
   
}else
{
   //如果是目录
}
Copy after login

Then let’s first look at the situation if it is not a directory. If it is not a directory, then we create the directory and perform file writing operations. First create a folder named study,

mkdir('study'); then open the file in read-write mode $open= fopen ('study/in.txt',”w ”);. Next, if this file is in writable modeif(is_writeable('study/in.txt')){},then write the content,if(fwrite($open,"Today is a beautiful day, must be happy! 《- -》")>0), if the writing is successful, close the file fclose($open), and output a successful prompt echo"<script>alert('<span style="color:#FF0000;"></span> Writing successfully');</script>";

Next, let's look at another situation, if it is a directory. If it is a directory, first determine whether the in.txt file exists in the directory ,

if(is_file('study/in.txt')){}, if it exists, determine whether the file is readableif(is_readable('study/in.txt')){}, if readable, output text information. echo file_get_contents('study/in.txt');And delete the file unlink('study/in.txt');and Delete directoryrmdir('study/in.txt');

Code:

<?php
     header(‘content-type:text/html;charset=utf8’);
     //判断study是否为文件夹目录
     If(!is_dir(‘study’))
     {
         //创建名为study的文件夹目录
         mkdir(‘study’);
//以读写的方式打开文件
$open = fopen(‘study/in.txt’,”w+”);
//如果此文件为可写模式
if(is_writable(‘study/in.txt’))
{
   //写入内容
  If(fwrite($open,”今天是美好的一天,一定要开心哦!《- -》”)>0);
   //关闭文件
  fclose($open);
   //输出成功的提示
   echo ”<script>alert(‘写入成成’);</script>”;
}
     }else{
        
           //判断目录是否存在in.txt文件
         //如果存在
         if(is_file(‘study/in.txt’))
         {
                           //判断文件是否可读
             If(is_readable(‘study/in.txt’))
             {  //如果可读
               //输出文本信息
   echofile_get_contents(‘study/in.txt’);
   //删除文件in.txt
   unlink(‘study/in.txt’);
   //删除目录
   rmdir();
   
             }
         }
     }
Copy after login

2. After the code is written, we test the effect. When opening the test.php file, a prompt window will pop up indicating that the writing is successful, click OK , when we open the local computer at this time, we will see an additional study directory. Open the study directory, and there is an in.txt file in it. Open in.txt and view it, and you will find that the content has been written successfully. When we refresh the browser again, we will find that the contents of the in.txt file have been read. In this way, the reading and writing of the file operation is completed.

The above is the entire article Content, I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Examples of implementing PageRank in php

php ID card recognition ORC method implementation

PHP implementation of two-dimensional array sorting according to specified fields

The above is the detailed content of How to operate php files. For more information, please follow other related articles on the PHP Chinese website!

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
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
PHP file operation function example: file last modification time PHP file operation function example: file last modification time Jun 21, 2023 am 11:43 AM

PHP is a widely used server-side programming language. It has powerful file operation capabilities, and the final modification time of a file is also a common requirement for file operations. Therefore, in this article, we will explore an example of PHP file operation function-how to get the last modification time of a file. Using the filemtime() function PHP provides a built-in function called filemtime(), which returns the last modification timestamp of a file. The timestamp is one from the UNIX epoch January 1970

PHP file operation function example: directory traversal PHP file operation function example: directory traversal Jun 21, 2023 am 10:04 AM

PHP is a very popular programming language that is widely used for web development, especially server-side development. File operation is an essential part of Web development. PHP provides a wealth of file operation functions. This article will introduce one of them: directory traversal. Directory traversal refers to traversing directories in the file system and obtaining files and subdirectories in the directories. In web development, directory traversal is often used for functions such as site map creation and file resource management, and it can also be used for website vulnerability detection and other aspects. Below, we will use examples to learn P

How to write a simple file management system using PHP How to write a simple file management system using PHP Sep 24, 2023 pm 02:04 PM

How to use PHP to write a simple file management system Preface: With the rapid development of the Internet, we come into contact with more and more various files in our daily lives, and it has become particularly important to manage these files effectively. As a commonly used server-side scripting language, PHP can help us build a simple and efficient file management system. This article will introduce in detail how to use PHP to write a file management system with basic functions and provide specific code examples. 1. Build the basic environment Before starting to write the file management system, we

Detailed explanation of PHP file functions: realizing file reading, writing and operating functions Detailed explanation of PHP file functions: realizing file reading, writing and operating functions Nov 20, 2023 pm 01:17 PM

PHP is a high-performance scripting language widely used for web development. In PHP, file operation is a very common and important function. This article will introduce in detail the use of file functions in PHP to help readers realize the reading, writing and operating functions of files. 1. Opening and closing files In PHP, the fopen function is used to open files. The syntax is as follows: $file=fopen("file path", "open mode"); where, file path

PHP file operation function example: file deletion PHP file operation function example: file deletion Jun 20, 2023 am 09:13 AM

PHP is a widely used open source programming language that is widely used in web development. In PHP, file operations are one of the very common operations. PHP provides a wealth of file operation functions, which can be used for operations such as reading and writing, creating and deleting files. This article will introduce an example of PHP file operation function: file deletion. In PHP, to delete a file, you can use the unlink() function. This function accepts a string parameter representing the path of the file to be deleted. For example, the following code will delete a file called

How to use PHP for file and folder operations How to use PHP for file and folder operations Sep 05, 2023 pm 06:48 PM

How to use PHP to implement file and folder operations PHP is a popular server-side scripting language. Its powerful file and folder operation functions make it the first choice for developers. This article will introduce in detail how to use PHP to implement common operations on files and folders, including creating, reading, writing, copying, deleting, and renaming. Creating Folders In PHP, you can use the mkdir() function to create new folders. This function accepts two parameters. The first parameter is the path of the folder to be created, and the second parameter is the path to the folder to be created.

Common functions for PHP file operations Common functions for PHP file operations Jun 16, 2023 pm 01:15 PM

PHP is a widely used open source programming language that is widely used in the field of web development. In web development, file operation is an essential part, so it is very important to be proficient in PHP's file operation functions. In this article, we will introduce some functions commonly used in PHP file operations. fopen() The fopen() function is used to open a file or URL and returns the file pointer. It has two parameters: file name and opening method. The opening mode can be "r" (read-only mode), "w" (write mode), "a"

PHP file operation function example: file copy PHP file operation function example: file copy Jun 20, 2023 pm 12:55 PM

In web development, file operation is a very common requirement, and PHP is a very powerful language that also provides a variety of file operation functions to meet this requirement. Among them, file copying is also one of the commonly used functions. This article will introduce the file copy function in PHP file operation function and give example code. First, let’s take a look at the file copy function in PHP. There are two main methods: copy() and file_put_contents(). Among them, the copy() function

See all articles