PHP - File System

> Procedural Languages > Php

1 - About

2 - Snippets

2.1 - Read a file content

file_get_contents

  • from an URL
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
  • from the same directory than the script
$homepage = file_get_contents(dirname(__FILE__). '/homepage.html');
echo $homepage;
Advertising

2.2 - Create directory

if (!file_exists($dir)) {
        mkdir($dir, $mode = 0777, $recursive = true);
}

where:

2.3 - Get the file extension

$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
 
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0