Chapter 6 php directory and file operations

  • 2020-05-10 17:50:55
  • OfStack

1. Directory operations
basename -- returns the file name part of the path
dirname -- returns the directory portion of the path
pathinfo -- returns information about the file path
realpath -- returns the normalized absolute pathname
 
<?php 
$path = 'demo1.php'; 
$path = realpath($path); 
echo basename($path); 
echo '<br>'; 
echo dirname($path); 
echo '<br>'; 
$array_path = pathinfo($path); 
echo 'basename : '.$array_path['basename'].'<br>'; 
echo 'dirname : '.$array_path['dirname'].'<br>'; 
echo 'extension : '.$array_path['extension'].'<br>'; 
echo 'filename : '.$array_path['filename'].'<br>'; 
?> 

Output:
demo1.php
D:\AppServ\www\Basic6
basename : demo1.php
dirname : D:\AppServ\www\Basic6
extension : php
filename : demo1

2. Disk, directory, and file counts
1. View file size and disk space
filesize -- get the file size
disk_free_space -- returns the available space in the directory
disk_total_space -- returns the total disk size for one directory
 
<?php 
$path ='demo2.php'; 
$path = realpath($path); 
$drive = 'c:'; 
echo round(filesize($path)/1024,2).'kb'.'<br/>'; 
echo round(disk_free_space($drive)/1024/1024/1024,2).'GB'.'<br/>'; 
echo round(disk_total_space($drive)/1024/1024/1024,2).'GB'.'<br/>'; 
?> 

output
0.26kb
10.61GB
30.01GB

2. Various times to obtain documents
fileatime -- get the last access time of the file
filectime -- get the inode modification time of the file
filemtime -- get the file modification time
 
<?php 
$file = realpath ( '../Basic5/demo3.php' ); 
$date_format = 'Y-m-d h:i:s'; 
echo 'lastest accessing time : '.date ( $date_format, fileatime ( $file ) ) . '<br>'; 
echo 'lastest change time : '.date ( $date_format, filectime ( $file ) ) . '<br>'; 
echo 'lastest modify time : '.date ( $date_format, filemtime ( $file ) ) . '<br>'; 
?> 

output
lastest accessing time : 2011-12-18 04:26:49
lastest change time : 2011-12-18 04:26:49
lastest modify time : 2011-12-18 04:29:15

3. File processing
There are two ways to read and write files:
1. Methods supported by all versions of php:
fopen -- open file or URL
fclose -- close 1 open file pointer
fwrite -- write file (secure for base 2 files)
Table 1. List of possible values for mode in fopen()

mode

instructions

'r'

With read-only mode open, point the file pointer to the file header.

'r+'

With read-write mode open, point the file pointer to the file header.

'w'

Write mode is turned on, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.

'w+'

With read-write mode open, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.

'a'

Write mode is turned on to point the file pointer to the end of the file. If the file does not exist, try to create it.

'a+'

With read-write mode open, point the file pointer to the end of the file. If the file does not exist, try to create it.

'x'

Create and open as write, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE with an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag for the underlying open(2) system call. This option is supported by PHP 4.3.2 and later and is only available for local files.

'x+'

Create and open it as a read/write, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE with an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag for the underlying open(2) system call. This option is supported by PHP 4.3.2 and later, and can only be used for local files.

 
<?php 
$fp = fopen('file1.txt','w'); 
$outStr = "my name is anllin,\r\nmy age is 29."; 
fwrite($fp,$outStr,strlen($outStr)); 
fclose($fp); 
?> 

output
my name is anllin,
my age is 29.
2. New method of adding php5
file_put_contents -- writes 1 string to a file
 
<?php 
file_put_contents('file2.txt',"my name is anllin,\r\nmy age is 29."); 
?> 

output
my name is anllin,
my age is 29.
How to read the contents of the document:

function

function

Fgetc()

Read 1 character and move the pointer to the next character

Fgets()

Read 1 line character to specify the length of 1 line display.

Fgetss()

Read 1 line from the file pointer and filter out the HTML flag

Fread()

Read quantitative characters

Fpassthru()

Outputs all remaining data from the file to the specified location

File()

Read the entire file into an array, grouped by lines

Readfile()

Read in 1 file and write to the output buffer

File_get_contents()

Read the entire file into 1 string

Feof()

Read the file function

File_exists()

Check to see if the file exists

The contents of the sample file file1.txt are as follows:
my name is anllin,
my age is 29.
fgetc -- reads characters from a file pointer
Demo.php
 
<?php 
$fp = fopen('file1.txt','r'); 
echo fgetc($fp); 
echo fgetc($fp); 
fclose($fp); 
?> 

Output:
my
fgets -- read 1 line from the file pointer
 
<?php 
$fp = fopen('file1.txt','r'); 
echo fgets($fp); 
echo fgets($fp); 
fclose($fp); 
?> 

output
my name is anllin, my age is 29.
fgetss -- read 1 line from the file pointer and filter out the HTML flag
 
<?php 
$fp = fopen('file3.txt','w'); 
$outStr = "my name is <b>anllin</b>"; 
fwrite($fp,$outStr,strlen($outStr)); 
fclose($fp); 
$ftp = fopen('file3.txt','r'); 
echo fgetss($ftp); 
fclose($ftp); 
?> 

Output
my name is anllin
fread -- read files (safe for use in base 2 files)
 
<?php 
$filename = 'file1.txt'; 
$fp = fopen($filename,'r'); 
$contents = fread($fp,filesize($filename)); 
echo $contents; 
fclose($fp); 
?> 

Output
my name is anllin, my age is 29.
fpassthru - outputs all remaining data at the file pointer
 
<?php 
$filename = 'file1.txt'; 
$fp = fopen($filename,'rb'); 
$leftSize = fpassthru($fp); 
echo $leftSize; 
fclose($fp); 
?> 

output
my name is anllin, my age is 29. 33
file - reads the entire file into an array
 
<?php 
$lines = file('file1.txt'); 
foreach ($lines as $line_num => $line) 
{ 
echo $line_num.' : '.$line.'<br>'; 
} 
?> 

output
0 : my name is anllin,
1 : my age is 29.
readfile -- output 1 file
 
<?php 
$size = readfile('file1.txt'); 
echo $size; 
?> 

output
my name is anllin, my age is 29.33
file_get_contents -- read the entire file into 1 string (php5.0 new)
 
<?php 
$contents = file_get_contents('file1.txt'); 
echo $contents; 
?> 

output
my name is anllin, my age is 29.
feof -- tests whether the file pointer reaches the end of the file
 
<?php 
$fp = fopen('file1.txt','r'); 
while(!feof($fp)) 
{ 
echo fgetc($fp); 
} 
fclose($fp); 
?> 

output
my name is anllin, my age is 29.
file_exists -- check to see if a file or directory exists
 
<meta http-equiv="content-type" content="text/html;charset=utf-8"/> 
<?php 
$filename = 'file1.txt'; 
if(file_exists($filename)) 
{ 
echo ' Perform file read and write operations '; 
} 
else 
{ 
echo ' The file you are looking for does not exist '; 
} 
?> 

output
Perform file read and write operations
filesize -- get the file size
 
<?php 
$file_size = filesize('file1.txt'); 
echo $file_size; 
?> 

output
33
unlink -- delete files
 
<?php 
$isDeleted = unlink('file3.txt'); 
echo $isDeleted; 
?> 

output
1
rewind -- rewind to the location of the file pointer
ftell -- returns the read/write location of the file pointer
fseek -- locate in the file pointer
 
<?php 
$fp = fopen ( 'file1.txt', 'r' ); 
fgetc ( $fp ); 
fgetc ( $fp ); 
echo ftell ( $fp ) . '<br>'; 
rewind ( $fp ); 
echo ftell ( $fp ) . '<br>'; 
fgetc ( $fp ); 
fgetc ( $fp ); 
echo ftell ( $fp ) . '<br>'; 
fseek($fp,0);//same as rewind ($fp) 
echo ftell ( $fp ) . '<br>'; 
?> 

output
2
0
2
0
The operation value of Flock

The operating value

meaning

LOCK_SH(previously 1)

Read and write locking. This means that the file can be Shared and read by others

LOCK_EX(previously 2)

Write lock. This is mutually exclusive and the file cannot be Shared

LOCK_UN(previously 3)

Release the existing lock

LOCK_NB(previously 4)

Prevents blocking when a lock is requested

flock - portable consultation file locking
 
<?php 
$path ='demo2.php'; 
$path = realpath($path); 
$drive = 'c:'; 
echo round(filesize($path)/1024,2).'kb'.'<br/>'; 
echo round(disk_free_space($drive)/1024/1024/1024,2).'GB'.'<br/>'; 
echo round(disk_total_space($drive)/1024/1024/1024,2).'GB'.'<br/>'; 
?> 
8
output
my name is anllin, my age is 29.
Directory handle operation
opendir -- open directory handle
readdir -- reads the entry from the directory handle
closedir -- close the directory handle
 
<?php 
$path ='demo2.php'; 
$path = realpath($path); 
$drive = 'c:'; 
echo round(filesize($path)/1024,2).'kb'.'<br/>'; 
echo round(disk_free_space($drive)/1024/1024/1024,2).'GB'.'<br/>'; 
echo round(disk_total_space($drive)/1024/1024/1024,2).'GB'.'<br/>'; 
?> 
9
output
.
..
.buildpath
.project
.settings
demo1.php
demo10.php
demo11.php
demo12.php
demo13.php
demo14.php
demo15.php
demo16.php
demo17.php
demo18.php
demo19.php
demo2.php
demo20.php
demo3.php
demo4.php
demo5.php
demo6.php
demo7.php
demo8.php
demo9.php
file1.txt
file2.txt
scandir - lists the files and directories in the specified path
 
<?php 
$file = realpath ( '../Basic5/demo3.php' ); 
$date_format = 'Y-m-d h:i:s'; 
echo 'lastest accessing time : '.date ( $date_format, fileatime ( $file ) ) . '<br>'; 
echo 'lastest change time : '.date ( $date_format, filectime ( $file ) ) . '<br>'; 
echo 'lastest modify time : '.date ( $date_format, filemtime ( $file ) ) . '<br>'; 
?> 
0
output
.
..
.buildpath
.project
.settings
demo1.php
demo10.php
demo11.php
demo12.php
demo13.php
demo14.php
demo15.php
demo16.php
demo17.php
demo18.php
demo19.php
demo2.php
demo20.php
demo21.php
demo3.php
demo4.php
demo5.php
demo6.php
demo7.php
demo8.php
demo9.php
file1.txt
file2.txt
rename -- rename a file or directory
 
<?php 
rename('demo1.php','demo01.php'); 
if(file_exists('demo01.php')) 
{ 
echo 'file rename success'; 
} 
else 
{ 
echo 'file rename fail'; 
} 
?> 

output
file rename success
rmdir -- delete directories
 
<?php 
rmdir('123'); 
if(file_exists('123')) 
{ 
echo 'delete file fail'; 
} 
{ 
echo 'delete file success'; 
} 
?> 

output
delete file success

Related articles: