Common file operations for php notes. Copy the code as follows :? Php common file operation functions part 1 file read/write, create, delete, rename, etc. before operating the file, determine whether the file is a file
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
?>
The http://www.bkjia.com/PHPjc/322562.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322562.htmlTechArticle code is as follows :? Php // common file operation functions // The first part of the file reads, writes, creates, deletes, renames, and so on // before starting the operation of the file, we first determine whether it is a document...