Common php functions 2 -- file operation_php tutorial

Source: Internet
Author: User
Common php functions 2: file operations. Common php functions 2-file operations 1. file type 1. filetype () gets the file type function. The return value of the parameter string (path + file name) is string, file (common file), and di php common function 2 is file operation.
I. file type
1. filetype ()
Get the file type function. the parameter is a string (path + file name)
The returned value is a string, file (common file), dir (directory), or unknown (unknown file)
2. is_file () is_dir ()
Returns a boolean value if it is of this type.
II. file attributes (the following function parameters are file names)
1. file_exists () check whether the file or directory exists
2. filesize (): get the file size. If an error occurs, false is returned.
3. whether is_readable () is readable
4. is is_writable () writable?
5. is is_executable () executable?
6. filectime () obtains the creation time.
7. filemtime () get the modification time
8. fileatime () get access time
9. stat () gets most of the file property values
10. clearstatcache () clears the file information cached by PHP.
III. parsing Directory
1. basename (path, [suffix]) returns the part of the file name in the path, and the second parameter is the extension (such as "php" or ". php "). if given, no extension is included in the returned value.
2. dirname (path) returns the directory name after removing the file name.
3. pathinfo () returns an associated array, including dirname (directory name), basename (basic name), extension (extension)
4. Traverse directories
1. open the specified directory in opendir () and return the directory handle that can be used by other Directory functions. False is returned for failure.
2. readdir () reads the specified directory. the parameter is the directory handle. return a file name of the current directory pointer position and move the pointer back. If no more files exist, false is returned.
3. closedir () closes the specified directory. the parameter is the directory handle.
4. rewinddir () returns the directory handle. the parameter is the directory handle and the Directory pointer is reset to the start position.
5. create and delete directories
1. create a new directory using mkdir (). the parameter is the directory name.
2. rmdir (): Delete a directory. The deleted directory can only be empty. if it is not empty, you must first enter the directory and delete the files using the unlink () function.
6. copy or move a directory
1. copy: php does not have a specific function. you must first create a directory mkdir () and then use the copy () function to copy each file.
2. move: copy the file first, and then delete the original directory.
7. open and close a file
1. fopen (filename, mode [, use_include_path [, zcontext]) open the file. the parameter is the file name and file mode. The third parameter is optional, if it is set to 1, PHP will consider configuring the path specified in the directive include_path. The fourth parameter is optional. set the allowed file name to start with the protocol name, such as http ://. Returns the file pointer. if the pointer fails, false is returned.
Summary:
R read-only
R + read/write
W write only (if the file exists, the original data will be deleted. if the file does not exist, the file will be created)
W + read/write (same as w)
X write (if the file exists, false is returned. if the file does not exist, it is created and only local)
X + read/write (same as x)
A write (the pointer points to the end of the file, and the file is created if the file does not exist)
A + WRITE (same as)
B binary mode
T text mode
2. close fclose ()
8. operation file content
1. fwrite (handle, string [, length]) writes a string. \ N \ r is the end character of the line. Returns the number of written characters. if the number of written characters fails, false is returned.
2. fread (handle, length) reads open files
3. file_get_contents () reads the file into a string
4. fgets (handle [, length]) returns a row
5. fgetc () return characters
6. file () reads a file into an array, with each action being an element.
7. readfile () reads a file and outputs it to the output buffer.
8. if feof () is used to determine whether the object has reached the end of the file, true is returned.
9. file_get_contents ()
10. access the remote file: activate the "allow_url_fopen" option in the configuration file. the set_time_limit () function controls the program running time to avoid timeout errors.
9. move the file pointer
1. ftell (handle) returns the current position of the file pointer
2. fseek (handle, offset [, whence]) moves the file pointer to the position specified by the offset parameter.
Parameter 3: add the second parameter to the current position of SEEK_CUR to specify the provided byte;
SEEK_END EOF is added with the offset byte. at this time, the offset value must be negative;
The effect of SEEK_SET offset is the same as that of SEEK_SET offset;
0 is returned for success, and-1 is returned for failure. For example, open with a or a +, always appended to the back, regardless of the file pointer position.
3. move rewind (handle) to the file switch
10. file locking mechanism (preventing multiple users from simultaneously accessing the same file and causing file confusion)
1. flock (handle, operation [, & wouldblock]) file lock operation. parameter 2: LOCK_SH shared lock, used for data reading; LOCK_EX exclusive lock, used for data writing; LOCK_UN released lock; LOCK_NB attaches a lock to prevent timed locking. Parameter 3: When it is set to 1, other processes are blocked during the lock.
10. copy and delete files
1. copy (this source file, destination file)
2. unlink (target file) delete the file
3. ftruncate (target file resource, truncation length) truncates the file to a specified length
4. rename (old file name, new file name) rename the file or directory
11. file upload and download
1. Global Array $ _ FILES
$ _ FILES ["myfile"] ["name"] original name, including the extension
$ _ FILES ["myfile"] ["size"] size of the uploaded file, in bytes
$ _ FILES ["myfile"] ["tmp_name"] temporary file name after Upload
$ _ FILES ["myfile"] ["error"] 0: successful; 1: The size exceeds the PHP configuration file limit; 2: The size exceeds the form limit; 3: the file upload is incomplete; 4: no files are uploaded.
$ _ FILES ["myfile"] ["type"] get the MIME type of the uploaded file
2. is_uploaded_file () determines whether it was uploaded through HTTPPOST.
3. move_uploaded_file (): move the uploaded file from the temporary location to the new location.
4. download header information processing
Header ('content-Type: image/gif '); MIME Type
Header ('content-Disposition: attachment; filename = "test.gif" '); header information, attachment, and file name
Header ('content-Length: 3390 '); size
Readfile('test.gif ');

Example 1: file type 1. filetype () obtain the file type function. the parameter is a string (path + file name) and the returned value is a string. file (common file), di...

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.