Skip to content

[WordPress] Use "file_get_contents()" To Load File

Last Updated on 2022-06-16 by Clay

When we using PHP to develop a WordPress plugin, we always need to read/load some file. Simply put, we can use file_get_contents() to do it.


Introduction of file_get_contents()

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )


  • $filename:the file path (could be URL)
  • $use_include_path:Use include_path of php.ini to find
  • $context: information context
  • $offset: start postion
  • $maxlen:maximum length

Use Method

The basically use method as follows:

$text = file_get_contents( "FILE_PATH" );
echo $text;


And you can print whole file. If you want to use relative position to read the file, you can use magic constant DIR to get the current directory path. Use the relative path, you can get the file position after the website has been deployed.

$text = file_get_contents( "FILE_PATH" );
echo $text;

References


Read More

Leave a ReplyCancel reply

Exit mobile version