Home >Backend Development >PHP Tutorial >'The fastest way to understand PHP programming' Lecture 5: PHP directory and file operations_PHP tutorial

'The fastest way to understand PHP programming' Lecture 5: PHP directory and file operations_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:57837browse

Example 15 Directory creation and deletion

Copy code The code is as follows:

$dirfile="file Clip";
$dirfile=iconv("UTF-8","GB2312",$dirfile);//Transcoding, otherwise you will see garbled characters in windows, but the program can operate normally and will be reversed when reading the directory. Only after I came over did I see the real name of the directory.
if(!file_exists($dirfile))//Used to determine whether a directory or file exists
mkdir($dirfile);//Create a directory
rmdir($dirfile);//Delete a directory, must It is an empty directory, otherwise all files in it must be deleted first. There are deletion methods below
echo "
";
?>

Example 16 File creation, deletion, Read and transfer array
Copy code The code is as follows:

$filename="file. txt";
$filename=iconv("UTF-8","GB2312",$filename);//Transcode, otherwise you will see garbled characters in windows
file_put_contents($filename,''); //Automatically create an empty file. If it already exists, delete it and create it again. Specifically, you can add file_exists judgment, which is simpler than functions such as fopen, fputs, and fclose.
unlink($filename);//Note that the file names are all encoded in GB2312
file_put_contents($filename, "Hello everyone! rnHello everyone!", FILE_APPEND);
//If you see it, write Enter two lines, the third parameter is optional, which means writing in incremental mode, otherwise clear the content and write again
echo file_get_contents($filename);//Ignore newlines and read the entire file
echo "< br>";
$arr=file($filename);//The file is read into the array line by line
print_r($arr);
echo "
";
readfile( $filename);//The file is output directly to the screen
echo "
";
?>

Example 17 Get url information and client ip address
Copy code The code is as follows:

//Get the domain name or host address
echo $_SERVER[ 'HTTP_HOST']."
";
//Get the web page address (middle part)
echo $_SERVER['PHP_SELF']."
";
//Get the URL Parameters (the back part of ?)
echo $_SERVER["QUERY_STRING"]."
";
//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 "
";
?>

Example 18 Get file modification timestamp, traverse directory files
Copy code The code is as follows:

$ filename="file.txt";
$filename=iconv("UTF-8","GB2312",$filename);
$passtime=time()-filectime($filename);//Creation time difference , inaccurate, generally not used
echo $passtime;
echo "
";
$passtime=time()-filemtime($filename);//Modify the time difference, used for update judgment, Buffering and other judgments
echo $passtime;
echo "
";
$dir="../";
print_r($arr=scandir($dir));// Get the names of all files and folders in the home directory
foreach($arr as $value){
if (!is_dir($dir.$value)) //Whether it is a directory, the directory also includes ".", " .."Two arrays, through judgment you can know whether it is a file or a directory, and what type of suffix name it is
echo iconv("GB2312","UTF-8",$value)."
rn ";
}
?>

Example 19 file contains
Copy code The code is as follows: >
$filename="file.txt";
@include($filename);//Included here, and then processed into html code by the server.
/*
@ means to ignore the error warning when it cannot be read. It is used before PHP statements and functions. Generally, the server-side error display is turned off, but there are some exceptions. In addition, you can also try-catch to catch exceptions, or use the file_exists function to first determine whether the file exists.
*/
require_once($filename);//Preprocessing inclusion, generally used for inclusion of configuration, functions, etc. Both functions can optionally be _once, emphasizing inclusion once.
//These four functions will be processed by the server as PHP code, simplifying repeated codes and are very commonly used. The readfile in Example 15 is directly output to the customer page as HTML
?>


It can be seen that PHP's function for operating file directories is relatively simple and powerful, and each function can be completed with just one line of code. This chapter does not introduce the copy function, you can try it yourself.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322475.htmlTechArticleExample 15 Directory creation and deletion copy code The code is as follows: ?php $dirfile="folder"; $dirfile= iconv("UTF-8","GB2312",$dirfile);//Transcoding, otherwise you will see garbled characters in windows, but the process...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn