'PHP programming the fastest to understand' lecture 5: PHP directory file operations

  • 2020-03-31 21:19:53
  • OfStack

Instance 15 directory creation and deletion
 
<?php 
$dirfile=" folder "; 
$dirfile=iconv("UTF-8","GB2312",$dirfile);//Transcode, otherwise you will see the Windows is messy code, but the program can operate normally, read the directory to see the real name of the directory in reverse.
if(!file_exists($dirfile))//To determine whether a directory or file exists
mkdir($dirfile);//Create a directory
rmdir($dirfile);//Delete directory, must be empty directory, otherwise to delete all the files in the first, there is a delete method
echo "<br>"; 
?> 

Instance 16 file creates, deletes, reads, and transfers an array
 
<?php 
$filename=" file .txt"; 
$filename=iconv("UTF-8","GB2312",$filename);//Transcode, otherwise you will see Windows is garbled
file_put_contents($filename,'');//Automatically creates an empty file, deletes it if it already exists, and adds the file_exists judgment, which is simpler than fopen, fputs, fclose, and so on.
unlink($filename);//Note that the file names are all GB2312
file_put_contents($filename," Everybody is good! rn Hello everyone! ",FILE_APPEND); 
//See, write two lines, the third parameter is optional, means to add to write, otherwise clean the content before writing
echo file_get_contents($filename);//Ignore the newline to read the entire file
echo "<br>"; 
$arr=file($filename);//The file is read into the array by line
print_r($arr); 
echo "<br>"; 
readfile($filename);//The file is output directly to the screen
echo "<br>"; 
?> 

Instance 17 gets url information and client IP address
 
<?php 
//Gets the domain name or host address
echo $_SERVER['HTTP_HOST']."<br>"; 
//Get the web address (middle)
echo $_SERVER['PHP_SELF']."<br>"; 
//Get url parameters (? Back part)
echo $_SERVER["QUERY_STRING"]."<br>"; 
//Source client IP address
if($_SERVER['HTTP_CLIENT_IP']){ 
$onlineip=$_SERVER['HTTP_CLIENT_IP']; 
}elseif($_SERVER['HTTP_X_FORWARDED_FOR']){ 
$onlineip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
}else{ 
$onlineip=$_SERVER['REMOTE_ADDR']; 
} 
echo $onlineip; 
echo "<br>"; 
?> 

Instance 18 gets the file modification timestamp and traverses the directory file
 
<?php 
$filename=" file .txt"; 
$filename=iconv("UTF-8","GB2312",$filename); 
$passtime=time()-filectime($filename);//Create time difference, not allowed, generally not used
echo $passtime; 
echo "<br>"; 
$passtime=time()-filemtime($filename);//Modify the time difference, used to update the judgment, buffer, and so on
echo $passtime; 
echo "<br>"; 
$dir="../"; 
print_r($arr=scandir($dir));//Gets the names of all the files and folders in the home directory
foreach($arr as $value){ 
if (!is_dir($dir.$value)) //Is it a directory? The directory also includes "..", ".." Two arrays, you can tell whether it's a file or a directory, and what kind of aftername is it
echo iconv("GB2312","UTF-8",$value)."<br>rn"; 
} 
?> 

Instance 19 file contains
 
<?php 
$filename=" file .txt"; 
@include($filename);//Included here, and then processed by the server into HTML code.
 
require_once($filename);//Preprocessing includes, generally used for configuration, function, etc. You can choose _once for both functions, emphasizing include once.
//All four of these functions are handled by the server in PHP code, simplifying repetitive code, and are commonly used. Instance 15's readfile outputs the HTML directly to the client page
?> 

As you can see, PHP manipulation of the file directory function is relatively simple and powerful, a function is only a line of code. This chapter does not introduce the copy function, you can try it yourself.

Related articles: