Vous êtes sur la page 1sur 9

Working with file and directories

ETL LABS PVT LTD – PHP 90


The file may be opened in one of the following modes

Modes Description

r Open a file for read only. File pointer starts at the beginning of the file

r+ Open a file for read/write. File pointer starts at the beginning of the file

Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File
w
pointer starts at the beginning of the file

Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File
w+
pointer starts at the beginning of the file

ETL LABS PVT LTD – PHP 91


The file may be opened in one of the following modes

Modes Description

Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file.
a
Creates a new file if the file doesn't exist

Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file.
a+
Creates a new file if the file doesn't exist

x Creates a new file for write only. Returns FALSE and an error if file already exists

x+ Creates a new file for read/write. Returns FALSE and an error if file already exists

ETL LABS PVT LTD – PHP 92


Open File - fopen() <?php
A better method to open files is with the $myfile =
fopen() function. This function gives you
fopen("webdictionary.txt", "r")
or die("Unable to open file!");
more options than the readfile()
3 echo
function.
fread($myfile,filesize("webdict
ionary.txt"));
We will use the text file, "webdictionary.txt", fclose($myfile);
during the lessons. ?>

ETL LABS PVT LTD – PHP 93


Read File - fread()
The fread() function reads from an
open file.
<?php
$myfile =
The first parameter of fread() contains
fopen("webdictionary.txt", "r") or
die("Unable to open file!"); the name of the file to read from and
echo 4 the second parameter specifies the
fread($myfile,filesize("webdictiona maximum number of bytes to read.
ry.txt"));
fclose($myfile); The following PHP code reads the
?> "webdictionary.txt" file to the end:

fread($myfile,filesize("webdict
ionary.txt"));

ETL LABS PVT LTD – PHP 94


Close File - fclose()
The fclose() function is used to close an
open file.

ETL LABS PVT LTD – PHP 95


Creating Directories
A new directory can be created in PHP using the
mkdir() function. This function takes a path to
the directory to be created. To create a directory
in the same directory as your PHP script simply
provide the directory name. To create a new
directory in a different directory specify the full
path when calling mkdir().

6
A second, optional argument allows the
specification of permissions on the directory
(controlling such issues as whether the directory
is writable):

<?php
$result = mkdir ("/path/to/directory",
"0777");
?>

ETL LABS PVT LTD – PHP 96


Deleting a Directory
Directories are deleted in PHP using
the rmdir() function. rmdir() takes
a single argument, the name of the
directory to be deleted.
7
The deletion will only be successful if
the directory is empty. If the directory
contains files or other sub-directories
the deletion cannot be performed until
those files and sub-directories are also
deleted.

ETL LABS PVT LTD – PHP 97


Finding and Changing the
Current Working Directory
It is unlikely that a web application will be able to
perform all of its file related tasks in a single
directory. For this reason, it is vital to be able to
both find out the current working directory, and
change to another directory from within a PHP
script.
8
The current working directory can be identified
using the getCwd() function:

<?php
$current_dir = getCwd();

echo "Current directory is


$current_dir";
?>

ETL LABS PVT LTD – PHP 98

Vous aimerez peut-être aussi