0% found this document useful (0 votes)
1 views

PHP Files Manipulation (1)

The document provides an overview of various PHP file manipulation functions, including readfile(), fopen(), fread(), fclose(), and others. It explains how to open, read, write, and delete files, as well as check for end-of-file and manage file pointers. Additionally, it covers functions for handling CSV files, checking file existence, and managing directories.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

PHP Files Manipulation (1)

The document provides an overview of various PHP file manipulation functions, including readfile(), fopen(), fread(), fclose(), and others. It explains how to open, read, write, and delete files, as well as check for end-of-file and manage file pointers. Additionally, it covers functions for handling CSV files, checking file existence, and managing directories.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PHP Files Manipulation

readfile()
PHP readfile() Function
The readfile() function reads a file and writes it to the output
buffer.
The PHP code to read the file and write it to the output buffer is as
follows (the readfile() function returns the number of bytes
read on success):

<?php
echo readfile(“filename.txt");
?>
PHP Open File - fopen()
• A better method to open files is with the fopen() function.
This function gives you more options than
the freadfile() function.
• The first parameter of fopen() contains the name of the
file to be opened and the second parameter specifies in
which mode the file should be opened. The following
example also generates a message if the fopen() function is
unable to open the specified file:
• 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
w Open a file for write only. Erases the contents of the file or creates a new file if it
doesn't exist. File pointer starts at the beginning of the file

a Open a file for write only. The existing data in file is preserved. File pointer starts at the
end of the file. 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
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it
doesn't exist. File pointer starts at the beginning of the file

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

x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
fread()
• The fread() function reads from an open file.

• The first parameter of fread() contains the name of the file to read from
and the second parameter specifies the maximum number of bytes to
read.
fread($myfile,filesize(“filename.txt"));
PHP Close File - fclose()
• The fclose() function is used to close an open file.
It's a good programming practice to close all files after you have finished with them. You don't
want an open file running around on your server taking up resources!
The fclose() requires the name of the file (or a variable that holds the
filename) we want to close:
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP Read Single Line - fgets()
The fgets() function is used to read a single line from a file.
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
Note: After a call to the fgets() function, the file pointer has moved to the
next line
PHP Check End-Of-File - feof()
The feof() function checks if the "end-of-file" (EOF) has been reached.
The feof() function is useful for looping through data of unknown length.
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
• Definition and Usage
• The die() function is an alias of the exit() function.

• Syntax
• die(message)

• Parameter Description
• message Required. A message or status number to print before terminating
the script. A status number will not be written to the output, just used as the exit
status.
PHP Read Single Character - fgetc()
The fgetc() function is used to read a single character from a file.
• <?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open
file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
• Note: After a call to the fgetc() function, the file pointer moves to the next character.
<?php
$file = fopen("test.txt","r");

while(! feof($file))
{
echo fgets($file). "<br />";
}

fclose($file);
?>

PHP copy() Function


Copy "source.txt" to "target.txt":

<?php
echo copy("source.txt","target.txt");
PHP delete() Function
Definition and Usage
There is no delete() function in PHP.
If you need to delete a file, use unlink() function.
PHP unlink() Function
<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
unlink("test.txt");
?>
Definition and Usage
The unlink() function deletes a file.
Syntax
unlink(filename, context)
Filename Required. Specifies the path to the file to delete
context Optional. Specifies the context of the file handle. Context is a
set of options that can modify the behavior of a stream
PHP dirname() Function
• Example
Return the path of the parent directory:
<?php
echo dirname("c:/testweb/home.php") . "<br />";
echo dirname("c:/testweb/home.php", 2) . "<br />";
echo dirname("/testweb/home.php");
?>
• The output of the code above will be:

c:/testweb
c:
/testweb
• Definition and Usage
• The disk_total_space() function returns the total size, in bytes, of the specified
filesystem or disk.

• Example
• Return the total size, in bytes, of the C: directory:

<?php
echo disk_total_space("C:");
?>
• The output of the code above could be:
119990349824
• Syntax
• dirname(path, levels)
• Parameter Values
• Parameter Description
• path Required. Specifies a path to check
• Levels Optional. An integer that specifies the number of parent directories to go up. Default
is 1
PHP disk_free_space() Function
Definition and Usage
• The disk_free_space() function returns the free space, in bytes, of the specified filesystem
or disk.
The diskfreespace() function is an alias of the disk_free_space() function.
• Example
• Return the free space, in bytes, of the C: directory:
<?phpThe output of the code above could be:
• PHP fgetcsv() Function
• Example
• Read and output one line from the open CSV file:
<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?>
• Definition and Usage
The fgetcsv() function parses a line from an open file, checking for CSV fields.
• file Required. Specifies the open file to return and parse a line from
• Length Optional. Specifies the maximum length of a line. Must be greater than the longest line
(in characters) in the CSV file. Omitting this parameter (or setting it to 0) the line length is not
limited, which is slightly slower. Note: This parameter is required in versions prior to PHP 5
• separator Optional. Specifies the field separator. Default is comma ( , )
• enclosure Optional. Specifies the field enclosure character. Default is "
• escape Optional. Specifies the escape character. Default is "\\"
<?php
$file = fopen("contacts.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
PHP fgetss() Function
• Example
• Read one line from the open HTML file (strip tags):
<?php
$file = fopen("test.htm","r");
echo fgetss($file);
fclose($file);
• PHP file_exists() Function
<?php
echo file_exists("test.txt");
?>
l() Function
• Example
• Return the current position of the read/write pointer in an open file:
<?php
$file = fopen("test.txt","r");

// Print current position


echo ftell($file);

// Change current position


fseek($file,"15");

// Print current position again


echo "<br>" . ftell($file);

fclose($file);
• PHP ftruncate() Function
• Definition and Usage :The ftruncate() function truncates an open file to the specified length.
• Syntax: ftruncate(file, size)
• file Required. Specifies the open file to truncate
• size Required. Specifies the new file size
• Truncates an open file to the specified length (100 bytes):
<?php
// Check filesize
echo filesize("test.txt");
echo "<br>";
$file = fopen("test.txt", "a+");
ftruncate($file,100);
fclose($file);
// Clear cache and check filesize again
clearstatcache();
echo filesize("test.txt");
?> The output of the code above will be: 792
100
• PHP rewind() Function
Example
• Rewind the position of the file pointer to the beginning of the file:
<?php
$file = fopen("test.txt","r");
//Change position of file pointer
fseek($file,"15");
//Set file pointer to 0
rewind($file);
fclose($file);
?>
• PHP rmdir() Function
• Example
Remove "images" directory:
<?php
$path = "images";
if(!rmdir($path)) {
echo ("Could not remove $path");
}
?>
• Definition and Usage
The rmdir() function removes an empty directory.

You might also like