Common File Operations for PHP notes

Source: Internet
Author: User

CopyCode The Code is as follows: <? PHP
// Common file operation functions

// The first part reads, writes, creates, deletes, renames, and so on.
// Run the script before you startCompositionFirst, we should determine whether a file is executable readable and writable.
$ File = "test.txt ";
If (file_exists ($ file) // whether the disk disconnection file exists
{
Echo "the file exists <br> ";
} 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 <br> ";
}
If (is_dir ($ file ))
{
Echo "is a directory <br> ";
}

If (is_executable ($ file ))
{
Echo "Executable File <br> ";
}
If (is_readable ($ file ))
{
Echo "readable file <br> ";
}
If (is_writable ($ file ))
{
Echo "file writeable <br> ";
}
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} <br>"; // output
Fwrite ($ FP, "I am a <a href = 'HTTP: // www.jianlila.com '> </a> asdddddddddddddddddddddddddddddddddddddxxxxxxx"); // added write
$ 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} <br> ";
File_put_contents ("test.txt", "I am <a href = 'HTTP: // www.aiwobama.com '> love my parents </a> asdddddddddddddddddddddddddddddxxxxxxxxx ");
// 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} <br> ";
$ Fp = fopen ("test.txt", "R ");
Echo "one character". fgetc ($ FP). "<br>"; // read one character
$ Fp = fopen ("test.txt", "R ");
Echo "A line". fgets ($ FP). "<br>"; // read a line of Characters
$ Fp = fopen ("test.txt", "R ");
Echo "remaining data ";
Fpassthru ($ FP );
Echo "<br>"; // the 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 <br> ";
Echo "file type". filetype ($ file). "<br> ";
// 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 _. "<br> ";
Echo "directory of the current file". dirname (_ file _). "<br> ";
Echo "current file name". basename (_ file _). "<br> ";
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.