PHP learning file processing and file Upload courseware 1th/2 page _php Tutorial

Source: Internet
Author: User
Tags echo date filegroup flock fread readfile rewind
PHP Processing of files
PHP in the application of server-side files, the related category is not only the user and the server database between the various connection access operations, but also through the PHP built-in file processing functions, for general file processing operations.

BaseName--Returns the file name portion of the path
Syntax format: $path = "/home/httpd/html/index.php";
$file = basename ($path); $file is set to "index.php"
$file = basename ($path, ". php"); $file is set to "index"

PathInfo (): Parse file current path
Syntax format: $path _parts = PathInfo ("/www/htdocs/index.html");
echo $path _parts["DirName"]. "\ n"; /www/htdocs file path
echo $path _parts["basename"]. "\ n"; index.html file and extension
echo $path _parts["extension"]. "\ n"; HTML file format

File types and related information
FileSize (): Calculate file Size (byte)
Syntax format: $bytes =filesize ("A.txt");
Echo $bytes round ($bytes/1024,2);
Fileatime (): File last access time (timestamp)
Syntax format: echo date ("Y-m-d G:i:sa", fileatime);
Filectime (): File settling time
Syntax format: echo date ("Y-m-d G:i:sa", filectime);
Filemtime (): File Last update time
Syntax format: echo date ("Y-m-d G:i:sa", filemtime);
Fileperms (): File attributes and Permissions 10 binary
Syntax format: Echo substr (Base_convert (Fileperms (a.txt), 10,8), 3);
Fileowner (): Uid of the file owner (useful only under Linux systems)
Syntax format: Echo fileowner ("A.txt");

Operation of the file
The ' R ' read-only mode opens, pointing the file pointer to the file header.
The ' r+ ' read-write mode opens, pointing the file pointer to the file header.
The ' W ' Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
The ' w+ ' read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
' A ' writes open, pointing the file pointer at the end of the file. If the file does not exist, try to create it.
The ' A + ' read-write mode opens, pointing the file pointer at the end of the file. If the file does not exist, try to create it.

' X ' is created and opened in writing, 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 specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used on local files.
' x+ ' is created and opened as read-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 specifies o_excl| for the underlying open (2) system call The o_creat tag is equivalent. This option is supported by PHP 4.3.2 and later versions and can only be used on local files.

fopen ()--open file or URL
Fclose ()--Close an open file pointer
Fread ()--Read file contents
Fwrite ()--Write file
Syntax format: if (! $f = @fopen ("file03.php", "X")) {//Open a file X method write
Die ("file read failed"); Read failed
}
Fwrite ($f, "kkkkkkkkkk"); Write something to that file.
Echo fread ($f, 10); Read the contents of this file
Fclose ($f); Close Open File


File ()--reads the entire document into an array
Syntax format: $arr =file ("file03.php");
Print_r ($arr); Reading a file returns an array

ReadFile ()-Reads a file and writes to the output buffer.
Syntax format: $str =readfile ("file03.php");
Echo $str; Output

The notation of a file register:
$f =fopen ("file03.php", "R");
$i =fread ($f, 1000);
echo "This is your {$i} second visit";
Fclose ($f);
$f =fopen ("file03.php", "w");
$i + +;
Fwrite ($f, $i);
Fclose ($f);
````````````````````````````````````````
File_get_contents () – reads the entire file into a string
Syntax format: file_get_contents (file name or URL)

File_put_contents () – writes a string to a file, and then calls fopen (), fwrite (), and fclose () functions
File_put_contents (file name, write data)


Feof ()--Test whether the file pointer is to the end of the file position
Ftell ()--Returns the position of the file pointer read/write
Syntax format ftell (file pointer)
Flock ()--Light consultation file lock
Syntax format: flock (file pointer, control parameter)
File pointer: is an already open (fopen) file pointer control parameter:
"Lock_sh" means to get a shared lock (read program), (PHP4.0.1 previous version set 1).
"LOCK_EX" means to obtain an exclusive lock (writer), (PHP4.0.1 was set to 2 in previous versions).
"Lock_un" means to release the lock (regardless of share or exclusive), (PHP4.0.1 was set to 3 in previous versions).
"LOCK_NB" means that if you do not want flock () to block when locked, then add this parameter to the control parameter

Fseek ()--Locating in the file pointer
Syntax structure: fseek (file pointer, move character count [, starting position constant])
File pointer: cannot be used to open the returned file pointer in fopen () in the format "http:/" or "ftp://".
Move the number of characters: when positive, move the file pointer forward a specified number: When negative, move the file pointer backward by the specified number:
Starting position constant:
Seek_cur-Sets the position to the current position.
Seek_set-Sets the position equal to the beginning of the file. (default value)
Seek_end-Set the location to the end of the file.


Rewind ()--Rewind 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 you open the file in append ("a" or "A +") mode, any data written to the file will always be appended, 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 ()--Group to get files
Syntax structure: Filegroup (file name)

chmod ()--Change file mode
Syntax structure: chmod (file name, privilege constant) 755 666

Chown ()--Change the owner of the file
Syntax structure: chown (file name, user)

Fileowner ()--Get the owner of the file
Syntax structure: Fileowner (file name)
Posix_getpwuid () to resolve it to a user name.



Copy ()--Copy file
Syntax structure: Copy (source file, destination file)
return type: bool, returns TRUE if successful, or FALSE if unsuccessful.
Parameter description: Copies the source file to the destination file.
Unlink ()--delete file
Syntax structure: unlink (target file)
return type: bool, returns TRUE if successful, or FALSE if unsuccessful.
Parameter description: Delete the specified target file
Rename ()--Rename a file or directory
Syntax structure: rename (old file name, new filename)
return type: bool, returns TRUE if successful, or FALSE if unsuccessful.
Parameter description: Attempt to rename the old file name to a new one.

File Property Handling
File_exists ()--check whether a file or directory exists
Syntax structure: file_exists (file name)
return type: bool, returns False if present returns True.
FileSize ()--Get file size
Syntax structure: filesize (file name)
Return type: Returns the number of bytes of file size and returns False if an error occurs.
FileType ()--Get file type
Syntax structure: filetype (file name)
Return type: Returns the file type. The possible values are fifo,char,dir,block,link,file and unknown. Error returns false
Is_dir ()--Determines whether a given file name is a directory
Syntax structure: is_dir (name)
Return type: Returns True if the file name exists and is a directory, otherwise false.
Is_executable ()--determine if the given file name is executable
Syntax structure: is_executable (name)
return type: FALSE If the file exists and the executable returns TRUE.
Is_file ()--determine if the given file name is a normal file
Syntax structure: is_file (name)
Return type: Returns TRUE if the file exists and is a normal file.
Is_link ()--Determines whether a given file name is a symbolic connection
Syntax structure: is_link (name)
Return type: Returns True if the file exists and is a symbolic connection.
Is_readable ()--Determines whether a given file name is readable
Syntax structure: is_readable (file name)
Return type: Returns TRUE if the file exists and is readable.
Is_writable ()--Determines whether a given file name can be written
Syntax structure: is_writable (file name)
Return type: Returns TRUE if the file exists and is writable.


Implementing directory Reads for the iterator interface
Standard method for iterator interfaces
Current (): Returns the value of the element in the list.
Next (): Used to move down one position in a list.
Valid (): Detects if there is still the next element in the current list, and returns True if any, otherwise false.
Rewind (): You can access the list of elements for the specified feature, and the pointer is set at the top when you begin operation iterator.

http://www.bkjia.com/PHPjc/319082.html www.bkjia.com true http://www.bkjia.com/PHPjc/319082.html techarticle PHP file processing PHP in the server-side file application, the related category is not only the user and the server database between the various connection access operations, can also be built in PHP files ...

  • 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.