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

Source: Internet
Author: User
Tags chmod constant file upload filegroup flock fread readable versions
PHP's handling of files
PHP in the application of server-side files, the relevant category is not only the user and server database of various connection access operations, can also be built through PHP file processing functions, for general file processing operations.

BaseName--Returns the file name part 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 (): Profiling 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 (): Calculates the size of a file (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 creation time
Syntax format: echo date ("Y-m-d G:i:sa", filectime);
Filemtime (): The last time the file was updated
Syntax format: echo date ("Y-m-d G:i:sa", filemtime);
Fileperms (): File properties and Permissions 10 system
Syntax format: Echo substr (Base_convert (Fileperms (a.txt), 10,8), 3);
Fileowner (): Uid of the owner of the file (useful only on 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.
' r+ ' read-write mode opens, pointing the file pointer to the file header.
The ' W ' 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 creating it.
' w+ ' read-write mode opens, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try creating it.
The ' a ' write mode opens, pointing the file pointer at the end of the file. If the file does not exist, try creating it.
An ' A + ' read-write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try creating 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 a e_warning level error message is generated. If the file does not exist, try creating it. This and specifies the 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 for local files.
' x+ ' is created and opened 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 a e_warning level error message is generated. If the file does not exist, try creating it. This and specifies the 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 for local files.

fopen ()--open file or URL
Fclose ()--Closes an open file pointer
Fread ()--Read the contents of the file
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"); What do you write to that file?
Echo fread ($f, 10); Read the contents of this file
Fclose ($f); Close Open File


File ()--reads the entire file into an array
Syntax format: $arr =file ("file03.php");
Print_r ($arr); Read file return array

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

The wording of a file register:
$f =fopen ("file03.php", "R");
$i =fread ($f, 1000);
echo "This is your first {$i} 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 calls fopen () in turn, fwrite () and fclose () functions
File_put_contents (file name, write data)


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

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


Rewind ()--Returns the position of the file pointer, which is where the file pointer moves to the beginning of the file.
Syntax structure: Rewind (file pointer)
Note: If you open the file in additional ("a" or "A +") mode, any data written to the file will always be appended, regardless of the location of the file pointer

CHGRP ()--changing the group to which the file belongs
Syntax structure: chgrp (file name, group name)

Filegroup ()--the group that gets the file
Syntax structure: Filegroup (file name)

chmod ()--changing file mode
Syntax structure: chmod (file name, permission constant) 755 666

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

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



Copy ()--Copy files
Syntax structure: Copy (source file, destination file)
return type: bool, returns TRUE if successful, and returns FALSE if it fails.
Parameter description: Copies the source file to the destination file.
Unlink ()--Deleting files
Syntax structure: unlink (destination file)
return type: bool, returns TRUE if successful, and returns FALSE if it fails.
Parameter description: Deletes the specified destination file
Rename ()--Renaming a file or directory
Syntax structure: rename (old file name, new file name)
return type: bool, returns TRUE if successful, and returns FALSE if it fails.
Parameter description: Try renaming the old file name to the new file name.

File attribute handling
File_exists ()-Check whether a file or directory exists
Syntax structure: file_exists (file name)
return type: BOOL, if present returns True, otherwise returns false.
FileSize ()--Get file size
Syntax structure: filesize (file name)
Return type: Returns the file size byte number, if error returns false.
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. False returns if error
Is_dir ()--Determines whether a given file name is a directory
Syntax structure: is_dir (name)
Return type: Returns TRUE if the filename exists and is a directory, otherwise returns false.
Is_executable ()--Determines whether a given file name is executable
Syntax structure: is_executable (name)
Return type: Returns TRUE if the file exists and is executable, or FALSE.
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 ()--to determine if a given filename is readable
Syntax structure: is_readable (file name)
Return type: Returns TRUE if the file exists and is readable.
Is_writable ()--to determine if a given filename is writable
Syntax structure: is_writable (file name)
Return type: Returns TRUE if the file exists and is writable.


Directory read to implement iterator interface
Standard method of Iterator interface
Current (): Returns the value of an element in the active list (lists).
Next (): Used to move down one position in a list.
Valid (): Detects if there is a next element in the current list, returns True if it exists, or returns false.
Rewind (): You can access the list of elements of the specified feature, and the pointer is set at the top when you start the operation iterator.
Current 1/2 page 12 Next read the full text

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.