PHP file://


Introduction

Various URL-style protocols can be used with filesystem functions with the help of corresponding built-in wrappers avalable in PHP. The stream_wrapper_register() function is also there to define custom wrapper.

Default wrapper in PHP is file:// and it represents local filesystem. If no other protocol is explicitly used, PHP parser treats it as filesystem wrapper. The filename argument to be given to filesystem functions fopen(), file_get_contents() etc uses file:// protocol by default.

When file name doesn't begin with forward or backward slash, or drive letter in Windows, its path is taken as relative to current directory. However, in fopen() and file_get_contents() functions, file name may be searched in locations mentioned in include_path directive.

The file:// wrapper supports simultaneous read/write operations, creating and removing directory, and renaming a file. Also, file access is not restricted by allow_url_fopen directive in php.ini configuration settings.

Examples

Representation of file name in different possible ways is as follows −

//absolute path

$file=fopen("C:/xampp/php/test/test.txt","w");

//relative path (assuming current working directory is c:\xampp\php, file is opened in tst subdirectory)

$file=fopen("test/test.txt","w");

//current path. File will be opened in c:\xampp\php\test directory assuming it as current directory

$file=fopen("test.txt","w");

//using file://protocolfor absolute path

$file=fopen("file:///c:/xampp/php/test/test.txt","w");

//using file://protocol for file in document root

$file=fopen("file://localhost/test/test.txt","w");

Updated on: 22-Sep-2020

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements