PHP File

PHP provides a wealth of document processing functions. File processing mainly include:

File creation /opening

  • fopen :(create and open) a file or URL address.

File Write

  • fwrite : Writes content to a file for safe use in binary files.
  • file_put_contents: Writing to a file equates to calling fopen, fwrite, and fclose in turn.

File Read

  • fread: Reads a file for safe use in a binary file.
  • fgets: Reads one line of data from the file and points the file pointer to the next line.
  • fgetc: reads the file data verbatim from the file until the file finishes.
  • file_get_contents: Read the entire file into a string.
  • file: The entire file is read into an array, where each cell is the corresponding line in the file.

Check if the file exists

  • file_exists: Check to see if the file or directory exists.

Check if the file is readable and writable

  • is_readable: Check that the file is readable.
  • is_writable: Check if the file is writable.
  • is_executable: Check if the file is executable.

File Copy

  • copy: Copy the file.

File Delete

  • unlink: Delete the file.

Get file size, type, modification time information

  • filesize: Get the file size.
  • filetype: Get the file type.
  • filemtime: Get the file modification time.

File pointer functions

  • fclose: Closes the file pointer.
  • feof: Test whether the file pointer to the end of the file location.
  • fseek: Locate in the file pointer.
  • rewind: Rewind the location of the file pointer.
  • ftell: Returns the location of the file pointer read /write.
  • *

Tip: Refer to the "PHP Directories" section for directory handling.


is_file() function is used to check whether a given file name is a normal file, if the file exists and the normal file returns TRUE, otherwise returns FALSE.


bool is_file(string filename)



Example output:


Refer to reading

is_dir function: Checks if a given file name is a directory.


When operating files and directories, you need to pay attention to the file directory read, write, delete permissions.

Copyright 2014-2017 by All Rights Reserved.