Common File Operations for php notes

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
// 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 "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.