A detailed _php tutorial based on PHP file operation

Source: Internet
Author: User
Introduction to Knowledge points:
1. Determine if a file or directory exists in bool
Copy CodeThe code is as follows:
File_exists (string filename)

2. Get the file name
Copy CodeThe code is as follows:
BaseName (filepath)

3. Open File
Copy CodeThe code is as follows:
fopen (Filename,mode)

4. Get File path information
Copy CodeThe code is as follows:
PathInfo (PATH)

5. Writing files
Copy CodeThe code is as follows:
Fwrite (resource,string)

6. Take the absolute path
Copy CodeThe code is as follows:
Realpath (filename)

7. Close the file
Copy CodeThe code is as follows:
Fclose ($handle)

8. Copying files
Copy CodeThe code is as follows:
Copy (Source,dest)

9. Read a row of data
Copy CodeThe code is as follows:
fgets (int handle[,int length])

10. Determine if it is a directory
Copy CodeThe code is as follows:
Is_dir (filename)

11. Read the entire file
Copy CodeThe code is as follows:
ReadFile (filename)

12. Open Directory
Copy CodeThe code is as follows:
Opendir (PATH)

13. Fetch File Size
Copy CodeThe code is as follows:
FileSize (filename)

14. Reading the Directory
Copy CodeThe code is as follows:
Readdir ($handle)

15. deleting files
Copy CodeThe code is as follows:
Unlink ()

16. Close the Directory
Copy CodeThe code is as follows:
Closedir ($handle)

17. Create a Directory
Copy CodeThe code is as follows:
mkdir (dirname)

18. Deleting a directory
Copy CodeThe code is as follows:
Unlink ()

19. Determine if a file or directory exists
Copy CodeThe code is as follows:
BOOL File_exists (string filename)

20. Determine if the file or directory exists, the presence returns True, or False
Format:
Copy CodeThe code is as follows:
if (file_exists ("Hello.txt"))
{
echo "File exists";
}

Open File

Format:
fopen (Filename,mode)
Description: Opens the specified file in the specified format
FileName: The file name to open
Mode: Open modes
fopen ("Hello.txt", "w");
means to open the Hello.txt file as written

Write a file

Format:
Fwrite (resource,string);
Description: Adds the specified content to the open file
Resource: Open File
String: What to write
Cases:
$handle = fopen ("Hello.txt", "w")//If a, data can be appended
Fwrite ($handle, "1\r\n")

Close File

Format:
Fclose ($handle)
Description: Close the Open file
Cases:
Copy CodeThe code is as follows:
$handle = fopen ("Hello.txt", "w");
Fclose ($handle);


Reading a row of data

Format:
fgets (int handle[,int length])
Description: Reads a length-1 character. If length is not specified, the default byte is 1KB.
If you encounter a newline, EOF, or have read length-1 characters, the program terminates,
Returns False when error occurs;
Cases:
Copy CodeThe code is as follows:
$handle = fopen ("Hello.txt", "R");
$buffer = fgets ($handle, 1024);
Echo $handle; Output one line of information


Read the entire file

Format:
ReadFile (filename)
Description: Reads the entire file and outputs it to the browser
Cases:
Copy CodeThe code is as follows:
ReadFile ("Hello.txt");
?>


Fetch file Size

Format:
FileSize (filename)
Description: Gets the specified file size, error returns false
Cases:
FileSize ("A.rar")

deleting files

Format:
Unlink ()
Description: Delete a file, return true if successful, or return false
Cases:
Unlink ("B.txt")

Create a Directory

Format:
mkdir (dirname)
Description: Create a directory
Example: mkdir ("NewFolder"); Create new folder under current directory

Delete Directory

Format:
RmDir (dirname)
Description: Delete a directory
Example: RmDir ("NewFolder");

Get file name

Format:
BaseName (filepath)
Description: Returns the file name from the specified path
Cases:
BaseName ("C:\mytools\a.txt")//Return to A.txt

Get File path information

PathInfo (PATH)
Note: Returns the file path information, the result is saved in the array, the array is labeled
DirName (Path), basename (file name), extension (extension)
Example: PathInfo ("C:\mytools\a.txt")

Take absolute path

Format:
Realpath (filename)
Description: Takes the absolute path of the specified file and returns False if it fails
Example: Realpath ("H.txt")//f:\apache\example\h.txt

Copying files

Format:
Copy (Source,dest)
Description: Copy the source file to Dest
Example: Copy ("H.txt", "Newfloder\a.txt")

Determine if it is a directory

Format:
Is_dir (filename)
Description: Determines whether a given file name is a directory. If filename exists and
Returns true for the directory, otherwise false is returned.
Cases:
Copy CodeThe code is as follows:
if (Is_dir ("NewFolder"))
{
echo "is the file directory";
}


Open Directory

Format: Opendir (PATH)
Description: Opens a specified file directory and returns a resource identifier
Cases:
$hand = Opendir (".")//Open root directory

Read Directory

Format:
Readdir ($handle)
Description: Read an open file directory stream
Readdir ($hand);

Close Directory

Format:
Closedir ($handle)
Description: Closes an Open Directory stream
Example: Closedir ($hand);

http://www.bkjia.com/PHPjc/327325.html www.bkjia.com true http://www.bkjia.com/PHPjc/327325.html techarticle Introduction to Knowledge Points: 1. Determine if a file or directory exists bool copy code code is as follows: file_exists (string filename) 2. Get the file name copy code code as follows: BaseName (filepath) 3 ...

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