Common file operations for php notes

Source: Internet
Author: User
Php file operations are required by phper. although there are few opportunities to use them, it seems that it is okay to sort out the note code and have detailed comments. The code is as follows:
// Common file operation functions

// The first part reads, writes, creates, deletes, renames, and so on.
// Determine whether a file is readable and writable before File operations.
$ File = "test.txt ";
If (file_exists ($ file) // whether the disk disconnection file exists
{
Echo "file exists
";
} Else
{
Echo "the file does not exist. you have created it ";
$ Fp = fopen ($ file, "w"); // Create in read-only mode
Fclose ($ fp );
}
If (is_file ($ file ))
{
Echo "is a file
";
}
If (is_dir ($ file ))
{
Echo "is a directory
";
}

If (is_executable ($ file ))
{
Echo "file executable
";
}
If (is_readable ($ file ))
{
Echo "file readable
";
}
If (is_writable ($ file ))
{
Echo "file writable
";
}
Chmod ($ file, 0777); // full permission
// Mode description: Number 1 indicates that the file is executable, number 2 indicates that the file is writable, and number 4 indicates that the file is readable. mode addition indicates the permission.
$ Fp = fopen ("test.txt", "a +"); // use the append read/write method to open
// When opening a remote file
// $ Fp = fopen ("test.txt", "a + B"); remember to add B;
$ Content = fread ($ fp, 70); // read 70 bytes
Echo "1. {$ content}
"; // Output
Fwrite ($ fp, "I recommend asdddddddddddddddddddddddddddddddddddddddddddxxxxxxxxx"); // write data by append
$ Content = file_get_contents ("test.txt"); // This function is recommended for reading remote files.
// $ Content = file_get_contents ("http://www.jianlila.com ");
Echo "2. {$ content}
";
File_put_contents ("test.txt", "I love my parents asdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddxxxxxxx ");
// Output to file
Fclose ($ fp); // close the file handle
$ Fp = fopen ("test.txt", "a + ");
$ Content = fread ($ fp, filesize ("test.txt "));
// Read all content filesize ($ file) // number of file bytes
Echo "3. {$ content}
";
$ Fp = fopen ("test.txt", "r ");
Echo "one character". fgetc ($ fp )."
"; // Read one character
$ Fp = fopen ("test.txt", "r ");
Echo "one line". fgets ($ fp )."
"; // Read a line of characters
$ Fp = fopen ("test.txt", "r ");
Echo "remaining data ";
Fpassthru ($ fp );
Echo"
"; // The remaining output data can be used to output binary files.
Copy ("test.txt", ".txt ");
// Copy a file
If (file_exists (" .txt "))
{
Unlink (" .txt ");
// Delete an object if any
}
Rename (" .txt", ". txt ");
// Rename the file

If (file_exists (" "))
{
Rmdir (""); // delete a folder
} Else
{
Mkdir (""); // create a folder
}


// Function for getting File information
$ File = "test.txt ";
Echo "file size". filesize ($ file). "bytes
";
Echo "file type". filetype ($ file )."
";
// The file here is not the 20-point fifo, char, dir, block, link, file, or unknown type.
$ Fp = fopen ($ file, "r"); // open the file
Print_r (fstat ($ fp); // Print the file information
Echo "current FILE path information". _ FILE __."
";
Echo "Directory of the current FILE". dirname (_ FILE __)."
";
Echo "current FILE name". basename (_ FILE __)."
";
Print_r (stat ($ file); // Print the file information

?>

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.