Front-end PHP Getting started -028-file operations-mastering levels

Source: Internet
Author: User
Tags fread readfile

As a beginner, we always do. The thing is CTRL + C and CTRL + V, the right mouse button to delete files, will control+c (or right) copy, paste files and new files, you can also set the file is read-only files and so on

    • Can I write a modification configuration file?
    • Is it possible to do PHP installation to detect file permissions
    • Isn't it possible to do a lot of different things like generating HTML files and so on

对于美工的各位同学,可能不太好理解,也就是说让程序去控制文件的操作,让我们变的更懒一些

Read file

Mastering the use of functions

ReadFile reading files

int ReadFile (string $ file name)

Function: Pass in a file path, output a file.

Code Demo:
The file is read as soon as it is passed in the file name or the specified file path.

  
 
  1. <?php
  2. //linux类的读了方式,它没有盘符的概念
  3. readfile("/home/paul/pangsir.txt");
  4. //windows类的读取方式,有的新机器是不允许读取根目录下的文件
  5. readfile("d:\\pangsir.html");
  6. //个人习惯这种
  7. //readfile("d:/pangsir.html");
  8. ?>

Operating effect:

Note: The preceding code has a slash in Windows and \\ may escape some characters. So, when we write, write a two slash.
问题:What are the special characters and escape characters we have learned?

File_get_contents Open File

Simply hit the file directly output, there is no open file, can assign value to a variable operation mode?

String file_get_contents (String filename)

Function:

To pass in a file or file path, open this file to return the contents of the file.
The contents of the file are a string.

  
 
  1. <?php
  2. $filename = ‘d:/pangsir.html‘;
  3. $filestring = file_get_contents($filename);
  4. echo ‘我要输出了--->>>>‘;
  5. echo $filestring;
  6. ?>

A file is opened and the contents of the file are output.

Extension Code: Note that because I am using HTML files, sometimes there is a problem oh!

  
 
  1. <?php
  2. //假设我们有一个多行的文件叫pangsir.html,没有的话你可以新建一个这个文件
  3. $filename = ‘d:/pangsir.html‘;
  4. //打开这个文件,将文件内容赋值给$filestring
  5. $filestring = file_get_contents($filename);
  6. //因为每一行有一个回车即\n,我用\n来把这个字符串切割成数组
  7. $filearray = explode("\n", $filestring);
  8. //把切割成的数组,下标赋值给$key,值赋值给$val,每次循环将$key加1。
  9. while (list($key, $val) = each($filearray)) {
  10. ++$key;
  11. $val = trim($val);
  12. //用的单引号,单引号不解释变量进行了拼接而已
  13. print ‘Line‘ . $key .‘:‘. $val.‘<br />‘;
  14. }
  15. ?>
fopen, fread, fclose operation Read file

File_get_contents Open the file in a simple, rude way. The following

Resource fopen (string $ file name, string mode)

String Fread (Resource $ operation resource, int read length)

BOOL Fclose (Resource $ operation Resource)

General operating methods:

1. Open a Resource

2. Working with related functions

3. Close Resources

fopen function
The function of the fopen function is to open the file, there are two main parameters:

    • Path to File Open
    • Mode of open File
      The return type is a resource type, and the first time we encounter the type of resource mentioned before the underlying type.
      The resource type requires additional functions to manipulate the resource. All resources have to be turned on and off.

fread function

    • The function of functions is to read open file resources.
    • Reads a file resource of a specified length and reads a portion of it backwards to move a portion.
    • To the end of the file.

fclose function

    • The function of the fclose function is to close the resource.
    • Resources have to be turned on and closed.
Mode Description
R Read-only opens, pointing the file pointer to the file header.
r+ Read-write mode opens, pointing the file pointer to the file header.
W The Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create
w+ Read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create
A Write to open, pointing the file pointer to the end of the file. If the file does not exist, try to create
A + Read-write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try to create the
X Creates and opens in writing, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create
x+ Creates and opens read-write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create

这个好好记住,很有用!

Contact Learning r模式 , next lesson we'll talk about a few other patterns when we write.
1. Open File

  
 
  1. <?php
  2. //你可以创建一个NoAlike.txt,以只读模式打开
  3. $fp = fopen(‘pangsir.txt‘, "r");
  4. //var_dump()操作一下$fp看看效果,输出的是不是只有类型提示的是resource
  5. var_dump($fp);
  6. ?>

2. Reading files

  
 
  1. <?php
  2. $fp = fopen(‘pangsir.txt‘, "r");
  3. //打开一个文件类型后,读取长度
  4. $contents = fread($handle, 1024);
  5. ?>

3. Close the file

  
 
  1. <?php
  2. $fp = fopen($filename, ‘r‘);
  3. $contents = fread($fp, 1024);
  4. fclose($fp);
  5. echo $contents;
  6. ?>

Other precautions:

Mode Description
T windows下将\n转为\r\n
B Binary open Mode


From for notes (Wiz)

Front-end PHP Getting started -028-file operations-mastering levels

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.