Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP file://
The file:// wrapper is PHP's default stream wrapper that represents the local filesystem. When no protocol is explicitly specified, PHP automatically uses the file:// wrapper for filesystem operations like fopen() and file_get_contents().
How file:// Works
The file:// wrapper provides access to the local filesystem and supports various operations ?
- Reading and writing files simultaneously
- Creating and removing directories
- Renaming files
- No restriction by
allow_url_fopendirective
Path Types
You can specify file paths in different ways with the file:// wrapper ?
Absolute Path
<?php
$file = fopen("C:/xampp/php/test/test.txt", "w");
fwrite($file, "Hello World!");
fclose($file);
?>
Relative Path
<?php
// Assuming current working directory is C:\xampp\php
$file = fopen("test/test.txt", "w");
fwrite($file, "Relative path example");
fclose($file);
?>
Current Directory
<?php
// File opened in current directory
$file = fopen("test.txt", "w");
fwrite($file, "Current directory file");
fclose($file);
?>
Using file:// Protocol Explicitly
You can explicitly use the file:// protocol in your file paths ?
Absolute Path with file://
<?php
$file = fopen("file:///c:/xampp/php/test/test.txt", "w");
fwrite($file, "Using file:// protocol");
fclose($file);
?>
Localhost Reference
<?php
$file = fopen("file://localhost/test/test.txt", "w");
fwrite($file, "Localhost file access");
fclose($file);
?>
Path Resolution
When a filename doesn't begin with a forward slash, backward slash, or drive letter (on Windows), PHP treats it as relative to the current directory. Functions like fopen() and file_get_contents() may also search locations specified in the include_path directive.
Conclusion
The file:// wrapper is automatically used by PHP for local filesystem access. You can specify paths as absolute, relative, or with explicit file:// protocol, making it flexible for various file operations.
