PHP file operation function details _ php Tutorial-PHP Tutorial

Source: Internet
Author: User
Php file operation function details. Php provides a large number of file operation functions. below I will summarize the php file functions for file reading and writing operations. For more information about these functions, see. Php provides a large number of functions for file operations. I will summarize the operations of php file functions on file reading and writing, for more information about these functions, see.

1. operate on files to obtain file information

The code is as follows:

// Open the file
$ File_path = "text.txt ";
If ($ fp = fopen ($ file_path, "r ")){
// Retrieve the file information
$ File_info = fstat ($ fp );
Echo"

";        print_r($file_info);        echo "
";

// Single fetch
$ File_size = $ file_info ['size'];
// The file size is calculated in bytes.
Echo "file size:". $ file_size;
Echo"
Last file access time :". date ("Y-m-d H: I: s", $ file_info ['atime ']); // atime indicates the time when the file was last accessed]
Echo"
Last File Modification time :". date ("Y-m-d H: I: s", $ file_info ['mtime']); // mtime indicates the time when the last file content was modified]
Echo"
Last File change time :". date ("Y-m-d H: I: s", $ file_info ['ctime']); // ctime indicates the time when the last File owner/file Group of the file was modified]

} Else {
Echo "failed to open the file ";
}

// Close the file. this is very important.
Fclose ($ fp );
?>

2. Method 2 for obtaining file information

The code is as follows:

// Obtain the file information
$ File_path = "text.txt ";
If (! File_exists ($ file_path )){
Echo "the file does not exist ";
Exit ();
}

Echo"
". Date (" Y-m-d H: I: s ", fileatime ($ file_path ));
Echo"
". Date (" Y-m-d H: I: s ", filemtime ($ file_path ));
Echo"
". Date (" Y-m-d H: I: s ", filectime ($ file_path ));

// Echo"
". Filemtime ($ file_path );
// Echo"
". Filectime ($ file_path );
?>

II. file read operations

The code is as follows:

// Read the file
$ File_path = "text.txt ";

If (! File_exists ($ file_path )){
Echo "the file does not exist ";
Exit ();
}

// Open the file
$ Fp = fopen ($ file_path, "a + ");
// Read the file
$ Content = fread ($ fp, filesize ($ file_path ));
Echo "file content is:
";
// By default, after the content is output to the webpage, it is not displayed with line breaks because the webpage does not recognize rn.
// All rn =>


$ Content = str_replace ("rn ","
", $ Content );
Echo $ content;

Fclose ($ fp );
?>


2. Method 2 for reading files

The code is as follows:


// Method 2

$ File_path = "text.txt ";
If (! File_exists ($ file_path )){
Echo "the file does not exist ";
Exit ();
}
$ Content = file_get_contents ($ file_path );

$ Content = str_replace ("rn ","
", $ Content );
Echo $ content;
?>


3. The third method is to read data cyclically (to deal with large files)

The code is as follows:


// The third method for reading (dealing with large files)

$ File_path = "text.txt ";
If (! File_exists ($ file_path )){
Echo "the file does not exist ";
Exit ();
}

// Open the file
$ Fp = fopen ($ file_path, "a + ");
// Define the number of bytes read each time
$ Buffer = 1024;
// Read one side. While determining whether it reaches the end of the file
While (! Feof ($ fp )){
// Read data by 1024 bytes
$ Content = fread ($ fp, $ buffer );
Echo $ content;
}

Fclose ($ fp );
?>


4. file reading application: when we connect to the database, we can configure the specified data to a file, and then obtain information in real time when PHP is running.

The code is as follows:

Db. ini file

Host = 127.0.0.1
User = root
Pwd = root
Db = test


Get file

The code is as follows:

$ Arr = parse_ini_file ("db. ini ");
Echo"

";    print_r($arr);    echo "
";

Echo $ arr ['host'];

// Connect to the database
$ Conn = mysql_connect ($ arr ['host'], $ arr ['user'], $ arr ['pwd']);

If (! $ Conn ){
Echo "error ";
}

Echo "OK ";
?>

III. file writing

The code is as follows:

// Write an object
$ File_path = "text.txt ";
If (! File_exists ($ file_path )){
Echo "the file does not exist ";
Exit ();
}
// "A +" append "w +" to the end of the file to re-write

$ Fp = fopen ($ file_path, "w + ");
$ Con = "rn Hello ";
For ($ I = 0; $ I <10; $ I ++ ){
Fwrite ($ fp, $ con );}

Echo "added successfully ";
Fclose ($ fp );


?>

2. The second method uses the file_put_contents function.

The code is as follows:


// Method 2
$ File_path = "text.txt ";
$ Content = "hello, worldrn ";

// Write a string to the file. the default value is [FILE_USE_INCLUDE_PATH] "w +" re-write.
File_put_contents ($ file_path, $ content, FILE_APPEND );

Echo "OK ";
?>


Refer file functions for reading and writing files. For more information about these functions, see ....

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.