PHP File Manipulation

Important Suggestions

One of the important features with server side languages, such as PHP, is the ability to read, write, and create files. By using the file functions in PHP this can be easily achieved.

Lesson

With a set of very intuitive file functions PHP can be easily implemented, and you will be able to edit text files from the comfort of your own website, eliminating the hassle of opening Dreamweaver or logging into your hosting provider.

I will first introduce to you the different file functions and what they do, and then I will show you how I use them.

Function Description
fopen($filepath, mode)

Establishes a connection to the specified file, with the passed mode. Modes are the states that the file can be viewed, or modified. Modes:

Mode Permissions Description
'r' Read only File is read from very beginning.
'r+' Read and Write File is read from beginning.
'w' Write only File is read from beginning; if there is no file it is created.
'w+' Read and Write File is read from beginning; if there is no file it is created.
'a' Write only File is read from the ending; if there is no file it is created.
'a+' Read and Write File is read from the ending; if there is no file it is created.
'x' Create and Write File is created and read from the beginning; if the file already exists an error will be thrown, E_WARNING, it can be suppressed by using '@'.
'x+' Create, Read and Write File is created and read from the beginning; if the file already exists an error will be thrown, E_WARNING, it can be suppressed by using '@'.
'c' Writing Only File is opened and read from the beginning; if there is no file it will be created.
'c+' Read and Write File is opened, written and read from the beginning, if there is no file it will be created.

fopen() - Implementation
<?php
$html_doc = fopen("index.html","w+")or die("<p>Error</p>");
?>
feof($filename)

feof() checks to see if the end of the file has been reached; it's good for reading files of unknown length. It is commonly used with loops, and returns a Boolean value.

feof() - Implementation
<?php
if(feof($filename)){
    echo "<p>File done reading!</p>";
}
?>
fgets($file, [int] length)

fgets() Returns a line from the file being read up until the length - 1 specified. The default line read length is 1024 bytes, it is more efficient reading a specified byte length when lines are in uniform length. If an error occurs in reading the data the fgets returns a Boolean value of false.

fgets() - Implementation
<?php
$line = fgets($filename, 256);
if($line){
    echo "<p>The file was successfully read, it says:<p>".$line;
}else{
echo "<p>Your file was not successfully read.<p>"; } ?>

fgetc($filename)

fgetc() Returns a character from the specified file parameter; this function is often used with a loop to read multiple characters from a small file, enhancing efficiency.

fgetc() - Implementation
<?php
echo 'Would you like to execute this command?(y/n) ';
$ans = fgetc(STDIN);
if($ans == 'y'){
    echo 'Command being executed...';
}else{
    echo 'Quitting...';
}
?>
fgetcsv($filename, [int] length, [string] $delimiter}

fgetcsv() Parses .csv file by delimiter, .csv elements are parsed into an array per line. The length parameter specifies the length of the largest line in the .csv file, can be set a very large number to avoid calculations, but impedes efficiency.

fgetcsv() - Implementation
<?php
$csv = fgetcsv($filename, 1000, ",");
$num = count($csv);
for($g=0;$g<$num;$g++){
    echo $csv[$g];
}
?>
fputcsv($filename, [array] $fields, [char] $delimiter)

fputcsv() Array is converted to a csv readable format, taking a delimiter parameter of one character in size. The .csv file is handled under the mode in which it is opened as.

fputcsv() - Implementation
<?php
$info = array(
    array('Bill', 'Sue'),
    array('34', '46'),
};
foreach($info as $f){
    fputcsv($filename, $f);
}
?>

Error

You need to log into your account to post comments!Sign In

Comments: 1 comment posted


hanningtonw8/25/14 5:47 PM

<p>Am requesting for a link which Mr.Glenn posted on &#8216;how to make a social website using php</p>