PHP File functions

Opening directory and listing files

See the following code:

<?php
  $dir = 'mydir';
  $myfiles = scandir($dir);
  print_r($myfiles);
?>

output:

Array
(
  [0] => .
  [1] => ..
  [2] => one.txt
  [3] => two.txt
)

To remove certain file name from the list, see the following code:

<?php
  $dir = 'mydir';
  $aExcept = array('.', '..');
  $myfiles = array_diff(scandir($dir), $aExcept);
  print_r($myfiles);
?>

This code lists all files except the ones listed in the $aExcept array:

Array
(
  [2] => one.txt
  [3] => two.txt
)

Getting filesize of a file

filesize function returns size of a file in bytes.

$f = 'files/debug.log';
echo filesize2bytes(filesize($f));

Deleting file contents

Deleting file contents To delete file contents and not the file, open the file in write mode and close is without writing anything.

$fh = fopen("file.txt", 'w');
fclose($fh);

Copying and renaming files

To copy files, use the copy() function. To rename files, use the rename() function.

$oldfile = 'file.txt';
$newfile = 'file2.txt';
copy($oldfile, $newfile);
rename($newfile,'file3.txt');

copy() will create a new file if it does not exist. If it exists, copy() would delete its contents.

Writing to file

The following code writes a string to a file called out.txt inside the data directory:

$f = "data/out.txt";
$fh = fopen($f, 'w') or die("can't open file");
$sData = "Some text\nmore text";
fwrite($fh, $sData);
fclose($fh);

Make sure that Apache has write permissions to the data directory.

Counting lines in a text file

The following simple code count the number of lines in a text file.

print count(file('file.txt'));

Reading file contents to an array and string

PHP allows you to easily read your file contents into an array or a string. To read into a string, use file_get_contents() function. To read into an array, use file() function. The file function would copy each line of content into a different array element.

Grocery List

bread
eggs
milk

grocery_list.txt

PHP code

<?php
  $afile = file('grocery_list.txt');
  $sfile = file_get_contents('grocery_list.txt');
  print '<br>file() returns contents of file as ' . gettype($afile);
  print '<br>file_get_contents() returns contents of file as ' . gettype($sfile);
  print '<br>';
  print_r($afile);
  print '<br>';
  print_r($sfile);
?>

grocery_list.php

Output

file() returns contents of file as array
file_get_contents() returns contents of file as string
Array ( [0] => bread [1] => eggs [2] => milk )
bread eggs milk 

Reading from file

Following is a code snippet which reads from a dta file. A dta file is an ms/ms data file format. It contains numbers separated by spaces. Something like:

1764.9410 2
120.0740 30.7619
136.0696 37.0476
175.1109 40.6191

To read from this file, we can use the following code:

<?php
$readfile = file(file.dta);

// read the first line and separate by space
$fields = split(" ",$readfile[0]);
print "$fields[0]m$fields[1]<hr>";

// read from the second to the last line, 
for ($i=1; $i <= count($readfile)-1; $i++) {
  // separate by space
  $fields = split(" ",$readfile[$k]);
  print "$fields[0]m$fields[1]<br>";
}
?>

file() function converts file contents into an array where each line can be accessed by an index (the line number). Next we split the line on some symbol, space in this case.

Technologies: