Home >Backend Development >PHP Tutorial >How to operate php files

How to operate php files

不言
不言Original
2018-07-05 16:27:293486browse

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
{
   //如果是目录
}

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"3f1c4e4b6b16bbbd69b2ee476dc4f83aalert(' Writing successfully');2cacc6d41bbb37262a98f745aa00fbf0";

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();
   
             }
         }
     }

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