PHP's processing of files
In the application of server-side files, the relevant scope lies not only in various connection and access operations between users and the server database, but also in general file processing operations through PHP’s built-in file processing functions.
basename - Return to the file name part in the path
Syntax format: $path = "/home/httpd/html/";
$file = basename($path); // $file is set to ""
$file = basename($path,".php"); // $file is set to "index"
pathinfo(): analyze the current path of the file
Syntax format: $path_parts = pathinfo("/www/htdocs/");
echo $path_parts["dirname"] . "\n"; /www/htdocs File path
echo $path_parts["basename"]. "\n"; File and extension
echo $path_parts["extension"]. "\n"; html File format
File type and related information
filesize(): calculates the size of the file (byte)
Syntax format: $bytes=filesize("");
echo $bytes round($bytes/1024,2);
fileatime(): The last time the file is accessed (timestamp)
Syntax format: echo date("Y-m-d g:i:sa",fileatime);
filectime(): file creation time
Syntax format: echo date("Y-m-d g:i:sa",filectime);
filemtime(): The last time the file is updated
Syntax format: echo date("Y-m-d g:i:sa",filemtime);
fileperms(): file attributes and permissions decimal
Syntax format: echo substr(base_convert(fileperms(),10,8),3);
fileowner(): file owner's uid (useful only on Linux systems)
Syntax format: echo fileowner("");
File operation
'r' Open read-only, pointing the file pointer to the file header.
'r+' Open the read and write mode, point the file pointer to the file header.
'w' Write mode is opened, point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create it.
'w+' Read and write mode is opened, pointing the file pointer to the file header and severing the file size to zero. If the file does not exist, try to create it.
'a' Write mode is opened, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
'a+' Read and write mode is opened, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
'x' Create and open as write, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.
'x+' Create and open in read-write mode, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.
fopen() -- Open file or URL
fclose() -- Close an open file pointer
fread() -- read file content
fwrite()--Write to file
Syntax format: if(!$f=@fopen("","x")){//Open a file and write the x method
die("File reading failed"); //Read failure
}
fwrite($f,"kkkkkkkkkkkkkkkkkkkkkkk"); //Write to that file What to write
echo fread($f,10); //Read the content of this file
fclose($f); Close to open the file
file() -- read the entire file into an array
Syntax format: $arr=file("");
print_r($arr); //Read the file and return the array
readfile() --Read in a file and write to the output buffer.
Syntax format: $str=readfile("");
echo $str; Output
How to write a file counter:
$f=fopen("","r");
$i=fread($f,1000);
echo “This is your {$i} visit”;
fclose($f);
$f=fopen("","w");
$i++;
fwrite($f,$i);
fclose($f);
````````````````````````````````````````
file_get_contents() – Read the entire file into a string
Syntax format: file_get_contents (file name or URL)
file_put_contents() –?Write a string to a file, and the same functions as calling fopen(), fwrite() and fclose() in turn
file_put_contents(file name, write data)
feof() -- Test whether the file pointer reaches the end of the file
ftell() -- Return the location of the file pointer read/write
Syntax format ftell (file pointer)
flock() -- Lightweight consultation file lock
Syntax format: flock (file pointer, control parameters)
File pointer: is a file pointer control parameter that has been opened (fopen):
"LOCK_SH" means to obtain a shared lock (reading program), (PHP4.0.1 previous version set 1).
"LOCK_EX" means to obtain an exclusive lock (write to the program), (set to 2 in previous versions of PHP4.0.1).
"LOCK_UN" means that the lock is to be released (whether shared or exclusive), (set to 3 in previous versions of PHP4.0.1).
"LOCK_NB" means that if you do not want flock() to be blocked during locking, add this parameter to the control parameter
fseek( ) --Locate in the file pointer
Syntax structure: fseek (file pointer, moving word number [, starting position constant])
File pointer: Cannot be used to open the returned file pointer in the format "http://" or "ftp://" in fopen().
Move the number of characters: When it is a positive number, move the file pointer forward and the specified number: When it is a negative number, move the file pointer backward and the specified number:
Start position constant:
SEEK_CUR - Set the position to the current position.
SEEK_SET - The setting position is equal to the beginning of the file. (default value)
SEEK_END - Set the position to the end of the file.
rewind( ) - Rewind back to the position of the file pointer, that is, move the file pointer to the beginning of the file.
Syntax structure: rewind (file pointer)
Note: If the file is opened in append ("a" or "a+") mode, any data written to the file will always be attached behind, regardless of the location of the file pointer.
chgrp( ) -- Change the group to which the file belongs
Syntax structure: chgrp (file name, group name)
filegroup( ) --The group that obtains the file
Syntax structure: filegroup (file name)
chmod( ) --Change file mode
Syntax structure: chmod (file name, permission constant) 755 666
chown( ) -- Change the owner of the file
Syntax structure: chown (file name, user)
fileowner( ) --The owner of the file
Syntax structure: fileowner (file name)
posix_getpwuid() to parse it into a username.
copy( ) -- Copy the file
Syntax structure: copy (source file, destination file)
Return type: bool type, if successful, return TRUE, and if failed, return FALSE.
Parameter description: Copy the source file to the destination file.
unlink( ) --Delete the file
Syntax structure: unlink (object file)
Return type: bool type, if successful, return TRUE, and if failed, return FALSE.
Parameter description: Delete the specified target file
rename( )--Rename a file or directory
Syntax structure: rename (old file name, new file name)
Return type: bool type, if successful, return TRUE, and if failed, return FALSE.
Parameter description: Try to rename the old file name to the new file name.
File attribute processing
file_exists( ) -- Check whether the file or directory exists
Syntax structure: file_exists (file name)
Return type: bool type, if there is a true, otherwise return false.
filesize( ) -- Get file size
Syntax structure: filesize (file name)
Return type: Returns the file size byte number, if an error occurs, return false.
filetype( ) -- Get file type
Syntax structure: filetype (file name)
Return type: Return file type. Possible values are fifo, char, dir, block, link, file, and unknown. If an error occurs, return false
is_dir( ) -- Determine whether the given file name is a directory
Syntax structure: is_dir (name)
Return type: Return true if the file name exists and is a directory, otherwise return false.
is_executable( ) -- Determine whether the given file name is executable
Syntax structure: is_executable (name)
Return type: Return TRUE if the file exists and is executable, otherwise return FALSE.
is_file( ) -- Determine whether the given file name is a normal file
Syntax structure: is_file(name)
Return type: Return TRUE if the file exists and is a normal file.
is_link( ) -- Determine whether the given file name is a symbolic connection
Syntax structure: is_link(name)
Return type: Return true if the file exists and is a symbolic connection.
is_readable( ) -- Determine whether the given file name is readable
Syntax structure: is_readable (file name)
Return type: Return TRUE if the file exists and is readable.
is_writable( ) -- Determine whether the given file name is writable
Syntax structure: is_writable (file name)
Return type: Return TRUE if the file exists and is writable.
Implement directory reading of iterator interface
Standard method of Iterator interface
current(): Returns the element value in the current list (list).
next(): Used to move one position downward in a list.
valid(): Detects whether there is another element in the current list. If so, return true, otherwise return false.
rewind(): You can access the list of elements of the specified feature. When you start operating the iterator, the pointer will be set at the top.
In the application of server-side files, the relevant scope lies not only in various connection and access operations between users and the server database, but also in general file processing operations through PHP’s built-in file processing functions.
basename - Return to the file name part in the path
Syntax format: $path = "/home/httpd/html/";
$file = basename($path); // $file is set to ""
$file = basename($path,".php"); // $file is set to "index"
pathinfo(): analyze the current path of the file
Syntax format: $path_parts = pathinfo("/www/htdocs/");
echo $path_parts["dirname"] . "\n"; /www/htdocs File path
echo $path_parts["basename"]. "\n"; File and extension
echo $path_parts["extension"]. "\n"; html File format
File type and related information
filesize(): calculates the size of the file (byte)
Syntax format: $bytes=filesize("");
echo $bytes round($bytes/1024,2);
fileatime(): The last time the file is accessed (timestamp)
Syntax format: echo date("Y-m-d g:i:sa",fileatime);
filectime(): file creation time
Syntax format: echo date("Y-m-d g:i:sa",filectime);
filemtime(): The last time the file is updated
Syntax format: echo date("Y-m-d g:i:sa",filemtime);
fileperms(): file attributes and permissions decimal
Syntax format: echo substr(base_convert(fileperms(),10,8),3);
fileowner(): file owner's uid (useful only on Linux systems)
Syntax format: echo fileowner("");
File operation
'r' Open read-only, pointing the file pointer to the file header.
'r+' Open the read and write mode, point the file pointer to the file header.
'w' Write mode is opened, point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create it.
'w+' Read and write mode is opened, pointing the file pointer to the file header and severing the file size to zero. If the file does not exist, try to create it.
'a' Write mode is opened, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
'a+' Read and write mode is opened, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
'x' Create and open as write, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.
'x+' Create and open in read-write mode, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT tag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.
fopen() -- Open file or URL
fclose() -- Close an open file pointer
fread() -- read file content
fwrite()--Write to file
Syntax format: if(!$f=@fopen("","x")){//Open a file and write the x method
die("File reading failed"); //Read failure
}
fwrite($f,"kkkkkkkkkkkkkkkkkkkkkkk"); //Write to that file What to write
echo fread($f,10); //Read the content of this file
fclose($f); Close to open the file
file() -- read the entire file into an array
Syntax format: $arr=file("");
print_r($arr); //Read the file and return the array
readfile() --Read in a file and write to the output buffer.
Syntax format: $str=readfile("");
echo $str; Output
How to write a file counter:
$f=fopen("","r");
$i=fread($f,1000);
echo “This is your {$i} visit”;
fclose($f);
$f=fopen("","w");
$i++;
fwrite($f,$i);
fclose($f);
````````````````````````````````````````
file_get_contents() – Read the entire file into a string
Syntax format: file_get_contents (file name or URL)
file_put_contents() –?Write a string to a file, and the same functions as calling fopen(), fwrite() and fclose() in turn
file_put_contents(file name, write data)
feof() -- Test whether the file pointer reaches the end of the file
ftell() -- Return the location of the file pointer read/write
Syntax format ftell (file pointer)
flock() -- Lightweight consultation file lock
Syntax format: flock (file pointer, control parameters)
File pointer: is a file pointer control parameter that has been opened (fopen):
"LOCK_SH" means to obtain a shared lock (reading program), (PHP4.0.1 previous version set 1).
"LOCK_EX" means to obtain an exclusive lock (write to the program), (set to 2 in previous versions of PHP4.0.1).
"LOCK_UN" means that the lock is to be released (whether shared or exclusive), (set to 3 in previous versions of PHP4.0.1).
"LOCK_NB" means that if you do not want flock() to be blocked during locking, add this parameter to the control parameter
fseek( ) --Locate in the file pointer
Syntax structure: fseek (file pointer, moving word number [, starting position constant])
File pointer: Cannot be used to open the returned file pointer in the format "http://" or "ftp://" in fopen().
Move the number of characters: When it is a positive number, move the file pointer forward and the specified number: When it is a negative number, move the file pointer backward and the specified number:
Start position constant:
SEEK_CUR - Set the position to the current position.
SEEK_SET - The setting position is equal to the beginning of the file. (default value)
SEEK_END - Set the position to the end of the file.
rewind( ) - Rewind back to the position of the file pointer, that is, move the file pointer to the beginning of the file.
Syntax structure: rewind (file pointer)
Note: If the file is opened in append ("a" or "a+") mode, any data written to the file will always be attached behind, regardless of the location of the file pointer.
chgrp( ) -- Change the group to which the file belongs
Syntax structure: chgrp (file name, group name)
filegroup( ) --The group that obtains the file
Syntax structure: filegroup (file name)
chmod( ) --Change file mode
Syntax structure: chmod (file name, permission constant) 755 666
chown( ) -- Change the owner of the file
Syntax structure: chown (file name, user)
fileowner( ) --The owner of the file
Syntax structure: fileowner (file name)
posix_getpwuid() to parse it into a username.
copy( ) -- Copy the file
Syntax structure: copy (source file, destination file)
Return type: bool type, if successful, return TRUE, and if failed, return FALSE.
Parameter description: Copy the source file to the destination file.
unlink( ) --Delete the file
Syntax structure: unlink (object file)
Return type: bool type, if successful, return TRUE, and if failed, return FALSE.
Parameter description: Delete the specified target file
rename( )--Rename a file or directory
Syntax structure: rename (old file name, new file name)
Return type: bool type, if successful, return TRUE, and if failed, return FALSE.
Parameter description: Try to rename the old file name to the new file name.
File attribute processing
file_exists( ) -- Check whether the file or directory exists
Syntax structure: file_exists (file name)
Return type: bool type, if there is a true, otherwise return false.
filesize( ) -- Get file size
Syntax structure: filesize (file name)
Return type: Returns the file size byte number, if an error occurs, return false.
filetype( ) -- Get file type
Syntax structure: filetype (file name)
Return type: Return file type. Possible values are fifo, char, dir, block, link, file, and unknown. If an error occurs, return false
is_dir( ) -- Determine whether the given file name is a directory
Syntax structure: is_dir (name)
Return type: Return true if the file name exists and is a directory, otherwise return false.
is_executable( ) -- Determine whether the given file name is executable
Syntax structure: is_executable (name)
Return type: Return TRUE if the file exists and is executable, otherwise return FALSE.
is_file( ) -- Determine whether the given file name is a normal file
Syntax structure: is_file(name)
Return type: Return TRUE if the file exists and is a normal file.
is_link( ) -- Determine whether the given file name is a symbolic connection
Syntax structure: is_link(name)
Return type: Return true if the file exists and is a symbolic connection.
is_readable( ) -- Determine whether the given file name is readable
Syntax structure: is_readable (file name)
Return type: Return TRUE if the file exists and is readable.
is_writable( ) -- Determine whether the given file name is writable
Syntax structure: is_writable (file name)
Return type: Return TRUE if the file exists and is writable.
Implement directory reading of iterator interface
Standard method of Iterator interface
current(): Returns the element value in the current list (list).
next(): Used to move one position downward in a list.
valid(): Detects whether there is another element in the current list. If so, return true, otherwise return false.
rewind(): You can access the list of elements of the specified feature. When you start operating the iterator, the pointer will be set at the top.
12Next pageRead the full text