Php file operation function set this article favorites a large number of php file operation functions, such as file opening, creating, deleting, changing groups, reading and writing files, uploading files, and opening remote files, write content to files and other instances.
$ Fp = fopen ("test.txt", "r ");
// Open the file in read-only mode and point the file pointer to the file header
$ Fp = fopen ("test.txt", "r + ");
// Open the file in read/write mode and point the file pointer to the file header
$ Fp = fopen ("test.txt", "w ");
// Open in writing mode. point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create
$ Fp = fopen ("test.txt", "w + ");
// Open the file in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create
$ Fp = fopen ("test.txt", "");
// Open the file in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create
$ Fp = fopen ("test.txt", "a + ");
// Open the file in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create
$ Fp = fopen ("test.txt", "wb + ");
// Open the binary file in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create
$ Fp = fopen ("c: est est.txt", "r ");
// On windows, escape each backslash in the file path or use a backslash
$ Fp = fopen ("http://www.domain.com/", "r ");
// Open the web address in read-only mode
$ Fp = fopen ("ftp: // username: password@domain.com/test.txt", "r ");
// Connect to the ftp server and log on to the server with username and password
/*
No output results for this instance code
*/
$ Fp = fopen ("test.txt", "r"); // open the file
$ Msg = fread ($ fp, filesize ("test.txt"); // read the file content to the variable
Print "$ msg"; // output file content
Fclose ($ fp); // close the file
//
$ Fp = popen ("test.txt", "r"); // open the file in read-only mode
$ Fp = popen ($ _ post ['command'], 'r ');
$ Read = fread ($ fp, 2096); // read content
Echo $ read; // Output Content
Pclose ($ fp); // close the file
// Use popen to execute the command. the command is a post-submitted parameter and returns the execution result. Note that if the command to be executed is not found, a valid resource is returned. It allows access to any error message returned by the shell. When security mode is enabled, only programs within safe_mode_exec_dir can be executed. The program path cannot contain the... component, that is, the command cannot be executed outside the specified directory.
$ Fp = popen ($ _ post ['command'], 'r'); // open the process file
$ Read = fread ($ fp, 2096); // read the process file pointer to the variable
Echo $ read; // Output Content
Pclose ($ fp); // closes the process file
//
$ File = "test.txt"; // definition file
If (file_exists ($ file) // checks whether the file exists.
{
Echo "clear cache below ";
}
Echo"
";
Clearstatcache (); // clear the file status cache
If (file_exists ($ file) // checks whether the file exists.
{
Die ('cleared successfully ');
}
//
$ Filename = "test.txt"; // definition file
$ User = "admin"; // defines the user
Chgrp ($ filename, $ group); // change the owner of the file/test/testfile.txt to "admin"
//
Chmod ("/test/testfile.txt", 0600 );
// Only the file owner has the read and write permissions
Chmod ("/test/testfile.txt", 0644 );
// The File owner has read and write permissions, and other users have read-only permissions.
Chmod ("/test/testfile.txt", 0755 );
// The File owner has all permissions, and other users have read and execute permissions.
Chmod ("/test/testfile.txt", 0750 );
// The File owner has all permissions, and the user group of the file owner has the read and execution permissions.
//
$ File = "test.txt"; // definition file
Delete ($ file); // delete an object
// When programming in php, the unlink function is generally used to delete files.
The following code uploads a File: Use the move_uploaded_file function to upload the file. if the file fails, use the copy function to upload the file to the specified directory and modify the directory attributes.
To use this code, you must have the file upload permission and define the $ path Upload path. In addition, you must have the upload content, which can be used independently without Output. the code is as follows:
If (function_exists ('move _ uploaded_file ') & move_uploaded_file ($ attachment, $ path ))
{
Chmod ($ path, 0666); // change the file access mode
$ Attachment = $ path;
}
Elseif (copy ($ attachment, $ path) // If move_upload_file, use destroy ()
{
Chmod ($ path, 0666); // change the file access attribute
$ Attachment = $ path;
} // Open source code phpfensi.com