test222
Comedy Algorithm Developer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Being a serious PHP programmer it is necessary to learn about how to manipulate files, because it gives you a great deal of tools for creating, uploading, and editing files. In this tutorial, we’re going to focus on working with the file system and directories in PHP. To start in in this lesson, let’s create first a folder inside the local server name and name it as “file”.
Then inside this folder we’re going to create a simple html file and add the following code, then save it as “basichtml.hml”.
At this we will create a new PHP file called “basicfile.php”. And take note, one of the most important things about working with files and directory is the magic constant __FILE__
and read as “underscore, underscore capital FILE underscore underscore”. Then lets add the following code.
Output after executing the code above:
This tutorial will serve as a basic fundamental block of building a file system, because before we write a file we want to check if the file is really exists or not. Or after we have written the file we really want to check if the file does exist. Or we want to check if file exists before we to want read.
Book traversal links for PHP File Handling: Basic File System
Then inside this folder we’re going to create a simple html file and add the following code, then save it as “basichtml.hml”.
- <
HMTL>
- <
title>
Basic HTML </
title>
- <
body>
- <
h3>
some content here.</
h3>
- </
body>
- </
HTML>
At this we will create a new PHP file called “basicfile.php”. And take note, one of the most important things about working with files and directory is the magic constant __FILE__
and read as “underscore, underscore capital FILE underscore underscore”. Then lets add the following code.
- <?php
- //it gives the full path where this is file is exactly located.
- echo
__FILE__
.
'<br/>'
;
- //it returns the result what exactly line is it located
- echo
__LINE__
.
'<br/>'
;
- //it gets the directory information about the file.
- echo
dirname
(
__FILE__
)
.
'<br/>'
;
- // it simply check if the file exist and were using ternary operators.
- echo
file_exists
(
__FILE__
)
? 'Yes'
:
'No'
;
- echo
'<br/>'
;
- //it gets the directory name of bascihtml.html file and return to yes or NO
- echo
file_exists
(
dirname
(
__FILE__
)
.
"/basichtml.html"
)
? 'Yes'
:
'No'
;
- echo
'<br/>'
;
- //it checks if it is a file or not and it return to Yes because it is a file.
- echo
is_file
(
dirname
(
__FILE__
)
.
"/basichtml.html"
)
? 'Yes'
:
'No'
;
- echo
'<br/>'
;
- //it checks if it is a directory but this time it
- //return to No because this is a file and it is not a directory.
- echo
is_dir
(
dirname
(
__FILE__
)
.
"/basichtml.html"
)
? 'Yes'
:
'No'
;
- echo
'<br/>'
;
- ?>
Output after executing the code above:

This tutorial will serve as a basic fundamental block of building a file system, because before we write a file we want to check if the file is really exists or not. Or after we have written the file we really want to check if the file does exist. Or we want to check if file exists before we to want read.
Book traversal links for PHP File Handling: Basic File System
- ‹ PHP File Handling: Accessing File
- Up
- PHP File Handling: Delete File ›