"PHP Programming Fastest understand" fifth: Php directory, file Operation _php Foundation

Source: Internet
Author: User
Tags arrays create directory php programming readfile
Instance 15 directory creation, deletion
Copy Code code as follows:

<?php
$dirfile = "folder";
$dirfile =iconv ("UTF-8", "GB2312", $dirfile);//transcoding, otherwise you will see the inside of Windows is garbled, but the program can operate normally, read the directory in turn to see the real name of the directory.
if (!file_exists ($dirfile))//is used to determine whether a directory or file exists
mkdir ($dirfile);//Create Directory
RmDir ($dirfile);//delete directory, must be empty directory, otherwise you have to delete all the files inside, followed by the deletion method
echo "<br>";
?>

Instance 16 file creation, deletion, reading, and forwarding arrays
Copy Code code as follows:

<?php
$filename = "file. txt";
$filename =iconv ("UTF-8", "GB2312", $filename);//transcoding, otherwise you will see that Windows is garbled
File_put_contents ($filename, ');//Automatically create empty files, if already exist delete and then create, concrete can increase file_exists judgment, than fopen, fputs, fclose functions simple.
Unlink ($filename);//Note the filename is GB2312 encoded
File_put_contents ($filename, "Hello everyone!" \ r \ n Hello, everyone! ", file_append);
See, write two lines, and the third argument is optional, which means write in an added way, otherwise empty the content and write to the
Echo file_get_contents ($filename);//Ignore line wrap read entire file
echo "<br>";
$arr =file ($filename);//file read to array in rows
Print_r ($arr);
echo "<br>";
ReadFile ($filename);//file output directly to screen
echo "<br>";
?>

Instance 17 Get URL information, client IP address
Copy Code code as follows:

<?php
Get domain name or host address
echo $_server[' Http_host ']. " <br> ";
Get page address (middle section)
echo $_server[' php_self ']. " <br> ";
Get URL parameters (? Back section)
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 get file modification timestamp, traverse directory file
Copy Code code as follows:

<?php
$filename = "file. txt";
$filename =iconv ("UTF-8", "GB2312", $filename);
$passtime =time ()-filectime ($filename);//Create Time difference, no, generally not
Echo $passtime;
echo "<br>";
$passtime =time ()-filemtime ($filename);//modification time difference, used to update judgment, buffer, etc.
Echo $passtime;
echo "<br>";
$dir = ". /";
Print_r ($arr =scandir ($dir))//Get all the file and folder names of the home directory
foreach ($arr as $value) {
if (!is_dir ($dir. $value))//Is the directory, the directory also includes the ".", "..." Two arrays, by which you can know whether a file or directory, and what type of sequence name
Echo iconv ("GB2312", "UTF-8", $value). " <br>\r\n ";
}
?>

The instance 19 file contains
Copy Code code as follows:

<?php
$filename = "file. txt";
@include ($filename);//included here, and then processed by the server into HTML code.
/*
@ Indicates that error warnings are ignored when used in PHP statements, functions, general server-side display errors are closed, but individual exceptions. In addition, we can try-catch catch the exception, or use the File_exists function to determine whether the file exists.
*/
Require_once ($filename);//preprocessing contains, generally used for configuration, functions, etc. included. Both of these functions can be selected _once, which is emphasized to include once.
These four functions will be processed by the server PHP code, simplifying duplicate code, very common. Instance 15 ReadFile directly when HTML is exported to the customer page
?>

You can see that the PHP operation file directory function is relatively simple and powerful, a function is a line of code to fix. This chapter does not introduce the copy function, you can try it yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.