Eight common file operation methods in PHP applications _php tutorial

Source: Internet
Author: User
Tags create directory
PHP's unique syntax mixes C, Java, Perl, and PHP's self-innovative syntax. PHP installs it to perform dynamic Web pages more quickly than CGI or Perl. Dynamic pages made with PHP are compared to other programming languages, and PHP is embedded in the HTML document to execute, which is much more efficient than CGI, which generates HTML markup entirely; PHP can also execute post-compilation code that compiles to encrypt and optimize code runs, making the code run faster. PHP has a very powerful feature, all CGI features PHP can be implemented, and support almost all the popular database and operating system.

Operation of files and directories

PHP is very handy for processing files and directories on the local server, but sometimes permissions and path-related issues can occur

1. Open File

Resource fopen (string filename, string mode [, bool Use_include_path [, Resource Zcontext]])

$handle = fopen (filename,mode)//Open file, return a handle to the resource representing this file

The file name can use a relative path or an absolute path can also use the Network Protocol mode, open mode has r+ww+aa+xx+ in the operation of binary files if the B tag is not specified, you may encounter some strange problems, including broken picture files and strange questions about characters.

For portability reasons, it is strongly recommended that you always use the B tag when opening a file with fopen ().

Here are a few ways to open files

$fp = @fopen (Log.txt, "RB");

$fp = @fopen (.. /log.txt, "RB");

$fp = @fopen ("http://www.runer.com.cn/default.htm", "RB");//You can also use protocols such as FTP and Ghoper, you must enable the Allow_url_fopen option in the php.ini file

Code section////////////////////////////////////////

$filename 1 = "userinfo.txt";//the file exists in the directory or include_path

$filename 2 = "test.txt";//directory or include_path does not exist for this file

$resource 1 = fopen ($filename 1, "RB");

@ $resource 2 = fopen ($filename 2, "RB");//Because this file does not exist in the directory and does not use or include_path to find the path containing the file, this operation will error, using the error suppressor @ can force the browser not to output error messages

if ($resource 1)

echo "Open file {$filename 1} succeeded";

if (! @fopen ($filename 2, "R"))

echo "Open file {$filename 2} unsuccessful";

//////////////////////////////////////////////////////////////////////////

---------------------The result of the output----------------------------------------

Open File Userinfo.txt Successful

---------------------------------------------------------------------

2. After using the file, you should explicitly tell PHP that you have finished using the file, and let the operating system ensure that all the contents of the file are flushed correctly from the buffer to the hard disk

Use Fclose () to close the file,

BOOL Fclose (resource handle)//close an open file pointer

3. Read the file, the fopen function of the mode parameter allows reading, PHP provides several functions to read data from the file

string fgets (int handle [, int length]) reads a row from the file pointer and attempts to fgets on the binary produce unpredictable results

If you do not specify a length, the default reads 1K data, encounters a newline character (included in the return value), EOF, or has read the length-1 byte and stops

String FGETSS (resource handle [, int length [, string allowable_tags]]) reads a row from the file pointer and filters out HTML tags

FGETC () reads a single character

Fread () reads any binary data

Code section////////////////////////////////////////

$handle = fopen ("Test.jpg", "RB");

$c;

while (!feof ($handle)) {

$contents. = @fread ($handle, 8192);//loop Read and merge it into a large file

}

Fclose ($handle);

//////////////////////////////////////////////////////////////////////////

---------------------The result of the output----------------------------------------

---------------------------------------------------------------------

4. Determine the status of the file read

Each file handle has a file pointer, or a cursor that indicates where the next operation will occur in the file, according to the mode parameter of the fopen function

The file pointer is initially located at the beginning of the file (0) or at the end of the file

Feof () can tell if the file has reached the end (the function returns true after the end)

The FileSize () function returns the size of the file 5. Writing files

Fwrite () function performs file write

Code section////////////////////////////////////////

$filename = Test.txt;

$somec;

First we want to make sure that the file exists and is writable.

if (is_writable ($filename)) {

In this example, we will use the Add mode to open the $filename,

Therefore, the file pointer will be at the beginning of the file,

That is where $somecontent will write when we use Fwrite ().

if (! $handle = fopen ($filename, a)) {

echo "Cannot open file $filename";

Exit

}

Write the $somecontent to the file we opened.

if (fwrite ($handle, $somecontent) = = = = FALSE) {

echo "Cannot write to file $filename";

Exit

}

echo "successfully wrote $somecontent to file $filename";

Fclose ($handle);

} else {

}

echo "File $filename not writable";

//////////////////////////////////////////////////////////////////////////

---------------------The result of the output----------------------------------------

Successfully add these words to the file to be written to the file Test.txt

---------------------------------------------------------------------

For binary data, you must specify a third parameter that contains the number of data bytes written to disk

$result = @fwrite ($fp, $binary _data,mb_strlen ($binary _data,8bit));

6. File permissions and other information

Is_readable ()//Determine if the file is readable

Is_writeable ()//Determine if the file is writable

Is_writable ()//Determine if the file is writable

Fileperms ()//Determine file Permissions (Unix-style file permissions test function)

File_exists ()//whether this file exists

Fileowner ()//determine who the file belongs to

Filegroup ()//determine which group the file belongs to

7. Delete and rename files

Unlink ()//delete files

Rename ()//rename file

8. Accessing the Directory

Directory access recommends using forward slash "/" compatible with Windows and UNIX systems

BaseName ()//Returns a file name that does not include path information

DirName ()//Returns the directory portion of the file name

Realpath ()//Accept relative path, return absolute path of file

PathInfo ()//extract directory name, base file name and extension for the given path

Opendir ()//Open Directory, return resource handle

Readdir ()//Read directory entry

Rewinddir ()//will read the pointer back to the beginning

Closedir ()//close Read handle

ChDir ()//change the current working directory during the current script execution

mkdir ()//Create Directory

RmDir () Delete directory

Code section////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

---------------------The result of the output----------------------------------------

Filename:web:filetype:dir

Filename:study:filetype:di

http://www.bkjia.com/PHPjc/485996.html www.bkjia.com true http://www.bkjia.com/PHPjc/485996.html techarticle PHP's unique syntax mixes C, Java, Perl, and PHP's self-innovative syntax. PHP installs it to perform dynamic Web pages more quickly than CGI or Perl. A dynamic page made with PHP ...

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