Personal blog is completed. Welcome to visit.
Rimmer lymoo's blog
Operation of PHP files
Open file
$fh = fopen("test.txt", "a");
read file
1.fread
$str = fread($fh, filesize("test.txt"));
filesize is to get the size of the file and return the type of string
2.fgets
fgets returns only one line, and the type returned is also a string. Return starts on the next line.
$str = fgets($fh);
$str = fgets($fh);
3.file
Note that the type returned by file is an array
$arr = file("test.txt");
4.file_get_contents
Getting file content with file_get_contents also returns a string and opens a web page with absolute paths
$str = file_get_contents("test.txt");
$str = file_get_contents("https://www.baidu.com");
File write
1.fwrite
fwrite($fh, "I am writing");
2.file_put_contents
file_put_contents("test.txt", "I am put Incoming");
Close file
fclose($fh);
Copy file
Copy (file to be copied, new file name);
copy("test.txt", "test2.txt");
rename file
Rename (original file name, new file name);
rename("test2.txt", "rename.txt");
Delete files
Unlink (file name to be deleted);
unlink("test.txt");
PHP get PV
What is PV?
PV (page view) is page views
Here I introduce the simplest principle and method of recording PV.
Principle: By judging whether there is a file recording the amount of pv, if not, create it, and write 1 in the file, if there is, get the value in the file, add 1, and then write to the file.
if (file_exists("pv.txt")){
$str = file_get_contents("pv.txt");
$str++;
echo "Current PV Quantity is".$str;
file_put_contents("pv.txt", $str);
} else {
echo "Current pv The quantity is 1.";
file_put_contents("pv.txt", 1);
}
Operations of PHP on File Directory
Open Directory
opendir(".");
read file
Like fgets, the first read only the first, the second to the second, and so on.
readdir($dh);
You can use the while loop to read
while ($file = readdir($dh)) {
echo $file."<hr/>";
}
Return all directories as arrays
$fileArr = scandir(".");
print_r($fileArr);
Close directory
closedir($dh);
create a file
mkdir("test");
Delete files
rmdir("test");
Display the File Catalog in tabular form
I won't say much about the direct examples here!
<?php
$dh = opendir(".");
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<table border="1">
<?php
while ($file = readdir($dh)) {
echo "<tr>";
echo "<td>{$file}</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
The results of this approach are the same.
<?php
$dh = opendir(".");
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<table border="1">
<?php
while ($file = readdir($dh)) {
?>
<tr>
<td><?php echo $file; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
The third way to display directories
<?php
@$url = $_GET["url"]; // @ Symbols can suppress errors, provided that the error does not affect the entire program.
echo $url;
if ($url)
$dh = opendir($url);
else
$dh = opendir(".");
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<table border="1">
<?php
while ($file = readdir($dh)) {
?>
<tr>
<td><?php echo $file; ?></td>
</tr>
<?php
}
?>
</table>
<a href="showDir3.php?url=..">Link to the top layer</a>
</body>
</html>
Some operations of PHP file directory
Get the last access time
fileatime();
date_default_timezone_set('PRC'); // Setting default time zone
$time = fileatime("file.php"); // The timestamp unit is seconds.
$time = date("Y-m-d H:i:s", $time); // Set the format of the printed time
echo $time;
Get the last modification time
filemtime();
$time = filemtime("file.php"); // The timestamp unit is seconds.
$time = date("Y-m-d H:i:s", $time); // Set the format of the printed time
echo $time;
Get the last Innode modification time
filectime();
ps:Innode includes modification permissions, etc.
$time = filectime("file.php"); // The timestamp unit is seconds.
$time = date("Y-m-d H:i:s", $time); // Set the format of the printed time
echo $time;
Get the filename
basename();
Notice that it takes a file name + suffix extension
$path = $_SERVER['REQUEST_URI']; // Get the file path in the server
$path = __FILE__; // Get the server root path file path
$basename = basename($path); // Get the filename
print_r(pathinfo($path)); // The returned array
Privilege modification of PHP files
chomd();
Usage: chomd (file, octal permission)
Permissions consist of four octal digits. The first is 0, which means that this is an octal digit.
The second representative is the authority of the owner, the third is the authority of the group in which the owner belongs, and the fourth is the authority of any person.
Privileges consist of 1,2,4, which can be accumulated for the superposition of privileges.
1 for executable permissions, 2 for writable permissions, and 4 for readable permissions
For example, to set the owner readable and writable, the owner's group can execute writable, and anyone can execute readable.
chmod("file.php", 0635);