Php file operation function set

Source: Internet
Author: User
Tags chmod file size file upload fread ftp php file

$ 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 "<p> ";
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. First, use the move_uploaded_file function to upload the file. If the upload 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 uploaded content. Used independently without Output Content
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;
}

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.