Home >Backend Development >PHP Tutorial >PHP and file operations_PHP tutorial
1. Directory operation
The first is the function to read from the directory, opendir(), readdir(), closedir(). When used, the file handle is opened first, and then iteratively listed:
$base_dir="filelist/";
$fso=opendir($base_dir);
echo $base_dir."
Sometimes you need to know the information of the directory. You can use dirname($path) and basename($path) to return the directory part and file name part of the path respectively. You can use disk_free_space($path) to return the remaining space to view the space.
Create command:
mkdir($path,0777): 0777 is the permission code, which can be set by the umask() function under non-window conditions.
rmdir($path): will delete the file with the path in $path.
2. File Operation
● New file
First, determine the permissions of the directory where the file you want to create is located; the recommended device is 777. Then, it is recommended to use an absolute path for the name of the new file.
$filename="test.txt";
$fp=fopen("$filename", "w+"); //Open the file pointer and create the file
if ( !is_writable($filename) ){
die("File:" .$filename. "Not writable, please check!");
}
//fwrite($filename, "anything you want to write to $filename.";
fclose($fp); //Close pointer
● Read file
First, check whether a file can be read (permission issue), or whether it exists. We can use the is_readable function to obtain the information.:
$file = 'dirlist.php';
if (is_readable($file) == false) {
die('The file does not exist or cannot be read');
} else {
echo 'existence';
}
?>
There is also file_exists (demoed below) to determine the existence of a file, but this is obviously not as comprehensive as is_readable. When a file exists, you can use
$file = "filelist.php";
if (file_exists($file) == false) {
die('File does not exist');
}
$data = file_get_contents($file);
echo htmlentities($data);
?>
However, the file_get_contents function is not supported on lower versions. You can first create a handle to the file, and then use a pointer to read all of it:
There is another way to read binary files:
$data = implode('', file($file));
● Write file
Same as reading a file, first check if it can be written:
$file = 'dirlist.php';
if (is_writable($file) == false) {
die("You have no right to write!");
}
?>
If you can write, you can use the file_put_contents function to write:
$file = 'dirlist.php';
if (is_writable($file) == false) {
die('I am a chicken feather, I can't');
}
$data = 'I am despicable, I want';
file_put_contents ($file, $data);
?>
The file_put_contents function is a newly introduced function in php5 (if you don’t know it exists, use the function_exists function to determine it first). Lower versions of php cannot be used. You can use the following method:
$f = fopen($file, 'w');
fwrite($f, $data);
fclose($f);
Replace it.
When writing a file, sometimes you need to lock it, and then write:
function cache_page($pageurl,$pagedata){
If(!$fso=fopen($pageurl,'w')){
$this->warns('Unable to open cache file.');//trigger_error
return false;
}
If(!flock($fso,LOCK_EX)){//LOCK_NB, exclusive lock
$this->warns('Unable to lock cache file.');//trigger_error
return false;
}
If(!fwrite($fso,$pagedata)){//Write byte stream, serialize to write other formats
$this->warns('Unable to write to cache file.');//trigger_error
return false;
}
flock($fso,LOCK_UN);//Release lock
fclose($fso);
Return true;
}
● Copy and delete files
Deleting files in PHP is very easy. Use the unlink function to simply operate:
$file = 'dirlist.php';
$result = @unlink ($file);
if ($result == false) {
echo 'The mosquitoes are gone';
} else {
echo 'Can't drive away';
}
?>
That’s it.
Copying files is also easy:
$file = 'yang.txt';
$newfile = 'ji.txt'; # The parent folder of this file must be writable
if (file_exists($file) == false) {
die ('The demo is not online and cannot be copied');
}
$result = copy($file, $newfile);
if ($result == false) {
echo 'Copy memory ok';
}
?>
You can use the rename() function to rename a folder. Other operations can be achieved by combining these functions.
● Get file attributes
Let me talk about a few common functions:
Get the latest modification time:
$file = 'test.txt';
echo date('r', filemtime($file));
?>
What is returned is the unix timestamp, which is commonly used in caching technology.
Related are also the time fileatime() and filectime() are used to obtain the last time accessed
$owner = posix_getpwuid(fileowner($file));
(non-window system), ileperms() obtains file permissions,
$file = 'dirlist.php';
$perms = substr(sprintf('%o', fileperms($file)), -4);
echo $perms;
?>
filesize() returns the file size in bytes:
//Output is similar: somefile.txt: 1024 bytes
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>
To get all the information of a file, there is a function stat() function that returns an array:
$file = 'dirlist.php';
$perms = stat($file);
var_dump($perms);
?>