Php file and directory operation functions (1/2)

Source: Internet
Author: User
Tags fread ftp html tags http request mkdir php tutorial readfile versions

1. File directory operations
The top-level stop is the root directory of the disk, with '/' or '//'
Current Directory ./
../Indicates the apache Directory, that is, the htdocs directory.
1. Create and delete the directory mkdir
Copy the code as follows:
<? Php Tutorial
If (mkdir ("./path", 0700) // create the path directory in the current directory
Echo "created successfully ";
?>

2. Get and change the current directory
You can use the getcwd () function to obtain the current working directory. This function has no parameters. If the job is successful, the current working directory is returned. If the job fails, FALSE is returned.
3. Open and close directory handles
Opendir ($ dir)
Closed ($ dir_handle)
4. Read directory content
Readdir (). This parameter is an opened directory handle, which can be traversed by a while loop.
5. Obtain the directories and files in the specified path.
Array scandir (string $ directory [, int $ sorting_order [, resource $ context])
Description: $ directory is the specified path. The $ sorting_order parameter is in ascending alphabetical order by default. If it is set to 1, it indicates that it is in descending order of letters.
$ Context is an optional parameter. It is a resource variable that can be generated using the stream_context_create () function. This variable stores data related to specific operation objects.
If the function runs successfully, an array containing all directories and file names in the specified path is returned. If the function fails, FALSE is returned.
2. General method of file operations
3. Open and close a file
1. Open the file
Resource fopen (string $ filename, string $ mode [, bool $ use_include_path [, resource $ context])
● $ Filename parameter. The fopen () function binds the name resource specified by $ filename to a stream.
● $ Mode parameter. The $ mode parameter specifies the mode in which the fopen () function accesses files. The values are shown in Table 4.5.
$ Mode
Description
'R'
Open the file in read-only mode and read from the file header
'R +'
Open a file in read/write mode and read/write from the file header.
'W'
Open the file in writing mode and point the file pointer to the file header. If the file already exists, delete the existing content. If the file does not exist, try to create it.
'W +'
Open the file in read/write mode and point the file pointer to the file header. If the file already exists, delete the existing content. If the file does not exist, try to create it.
'A'
Open the file in writing mode and point the file pointer to the end of the file. If the file has content, it will start from the end of the file. If the file does not exist, try to create it.
'A +'
Open the file in read/write mode and point the file pointer to the end of the file. If the file already contains content, it will be read and written from the end of the file. If the file does not exist, try to create it.
'X'
Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This option is supported by PH and later versions and can only be used for local files
'X +'
Create and open the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This option is supported by PH and later versions and can only be used for local files
'B'
Binary mode, used to connect to the end of other modes. If the file system can distinguish between binary files and text files (Windows, but UNIX is not), you need to use this option. We recommend that you always use this option for maximum portability.

● $ Use_include_path parameter. If you need to search for files in include_path (include path of PHP, configure File settings of PHP,
You can set the value of the optional parameter $ use_include_path to 1 or TRUE. The default value is FALSE.
● $ Context parameter. The optional $ context parameter is used only when a file is remotely opened (for example, opened through HTTP). It is a resource variable,
Data related to the specific operation object of the fopen () function is saved. If fopen () opens an HTTP address,
This variable records the HTTP request type, HTTP version, and other header information. If the FTP address is enabled,
It may be recorded in the passive/active FTP mode.
2. Close the file
Bool fclose (resource $ handle)
4. File writing
You need to open the file before writing the file. If it does not exist, you need to create it first. Generally, you can use the fopen () function to create the file.
● Fwrite (). After the file is opened, the content is written to the file.
Int fwrite (resource $ handle, string $ string [, int $ length])
Description: The $ handle parameter is the written file handle,
$ String is the string data to be written into the file,
$ Length is an optional parameter. If $ length is specified, data written into the first $ length byte in $ string is stopped.
● File_put_contents () function. PHP 5 also introduces the file_put_contents () function. This function has the same function as calling the fopen (), fwrite (), and fclose () functions in turn. The syntax format is as follows:
Int file_put_contents (string $ filename, string $ data [, int $ flags [, resource $ context])
Description: $ filename is the name of the file to write data.
$ Data is the string to be written, and $ data can also be an array, but not a multi-dimensional array.
When using FTP or HTTP to write data to a remote file, you can use the optional parameters $ flags and $ context, which are not described here.
After successful writing, the function returns the number of written bytes. Otherwise, FALSE is returned.
● Fputcsv () function. Csvis a commonly used file format, which generally uses .csv as the extension. The CSV format regards a row of a file as a record, and the fields in the record are separated by commas.
In PHP, you can use the fputcsv () function to format the specified array into contents that conform to the CSV file format, and write the current row pointed to by the file pointer. The syntax format is as follows:
Int fputcsv (resource $ handle [, array $ fields [, string $ delimiter [, string $ enclosure])
Description: The $ handle parameter is the file handle to be written.
The $ fields parameter is an array to be formatted.
The optional $ delimiter parameter is used to set the field delimiter (only one character is allowed). The default value is comma.
Optional $ enclosure parameter field surround character (only one character is allowed), double quotation marks by default
5. File reading
1. Read arbitrary length
The fread () function can be used to read file content. The syntax format is as follows:
String fread (int $ handle, int $ length)
Note: The $ handle parameter is an opened file pointer,
$ Length indicates the maximum number of bytes to read. The maximum value of $ length is 8192.
If you encounter an EOF before reading $ length, the read characters are returned and the read operation is stopped.
If the read is successful, the string to be read is returned. If an error occurs, FALSE is returned.
Note: When the file content is displayed after the file is read, the text may contain characters that cannot be directly displayed, such as HTML tags.
In this case, you must use the htmlspecialchars () function to convert the HTML tag to an object to display characters in the file.
2. Read the entire file
● File () function. The file () function is used to read the entire file into an array. The syntax format is as follows:
Array file (string $ filename [, int $ use_include_path [, resource $ context])
Note: This function is used to return objects as an array. Each unit in the array is a corresponding line in the file, including line breaks,
If it fails, FALSE is returned. The $ filename parameter is the name of the file to be read. The $ use_paie_path and $ context parameters have the same meaning as previously described.
● Readfile () function. The readfile () function is used to output the content of a file to the browser. The syntax format is as follows:
Int readfile (string $ filename [, bool $ use_include_path [, resource $ context])
● Fpassthru () function. The fpassthru () function can read the given file pointer from the current position to EOF and write the result to the output buffer.
To use this function, you must first use the fopen () function to open the file, and then pass the file pointer as a parameter to the fpassthru () function,
The fpassthru () function sends the file content pointed to by the file pointer to the standard output. If the operation is successful, the number of bytes is returned. Otherwise, FALSE is returned.
● File_get_contents () function. The file_get_contents () function can read the entire or part of the file content to a string,
The function is the same as calling the fopen (), fread (), and fclose () functions in turn. The syntax format is as follows:
String file_get_contents (string $ filename [, int $ offset [, int $ maxlen])
Description: $ filename is the name of the file to be read. The optional parameter $ offset can be used to specify the offset starting from the file header,
The function returns the content with a length of $ maxlen starting from the position specified by $ offset. If it fails, the function returns FALSE.

Homepage 1 2 Last page
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.