PHP file operation set of functions

Source: Internet
Author: User
Tags access properties fread
This article collects a lot of PHP file operation functions such as file Open, create, delete, notconsistent group, read write file, file upload and open remote file, write the content to file instance.

$FP =fopen ("Test.txt", "R");

Open a file as read-only, pointing the file pointer to the file header

$FP =fopen ("Test.txt", "r+");

Open the file as read-write, point the file pointer to the file header

$FP =fopen ("Test.txt", "w");

The Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create

$FP =fopen ("Test.txt", "w+");

Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create

$FP =fopen ("Test.txt", "a");

Opens as write, pointing the file pointer to the end of the file. If the file does not exist, try to create

$FP =fopen ("Test.txt", "A +");

Open as read-write, pointing 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 as written, point the file pointer to the file header, and truncate the file size to zero. If the file does not exist, try to create

$FP =fopen ("C:est est.txt", "R");

On the Windows platform, to escape each backslash in the file path, or use a slash

$FP =fopen ("http://www.domain.com/", "R");

Open web address as read-only

$FP =fopen ("Ftp://username:password@domain.com/test.txt", "R");

Connect to the FTP server to log on to the server username and password

/*

No output results for this instance code

*/

$FP =fopen ("Test.txt", "R"); Open File

$msg =fread ($fp, FileSize ("test.txt")); Read file contents to variable

print "$msg"; Output file contents

Fclose ($FP); Close File

//

$FP =popen ("Test.txt", "R"); Open a file as read-only

$fp =popen ($_post[' command '], ' r ');

$read =fread ($fp, 2096); Read content

Echo $read; Output content

Pclose ($FP); Close File

Here the command is executed with Popen, and the command is a post-submitted parameter and returns the execution result. It is important to note that if you do not find the command to execute, a legitimate resource is returned. It allows access to any error messages returned by the shell. When Safe mode is turned on, only programs that are within Safe_mode_exec_dir can be executed and cannot be included in the path to the program: The command cannot be executed outside of the specified order.

$fp =popen ($_post[' command '], ' r '); Open a process file

$read =fread ($fp, 2096); Reading a process file pointer to a variable

Echo $read; Output content

Pclose ($FP); Close process Files

//

$file = "Test.txt"; Definition file

if (file_exists ($file))//Determine if the file exists

{

echo "Clear cache below";

}

echo "<p>";

Clearstatcache (); Clear the file state cache

if (file_exists ($file))//Determine if the file exists

{

Die (' cleared ');

}

//

$filename = "Test.txt"; Definition file

$user = "admin"; Define 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 read and write permissions

chmod ("/test/testfile.txt", 0644);

The file owner has read and write permissions, and the other user has readonly permissions

chmod ("/test/testfile.txt", 0755);

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 file owner's user group has read and execute permissions

//

$file = "Test.txt"; Definition file

Delete ($file); deleting files

In PHP programming, you typically use the Unlink function to delete files.

The following code implements the file upload function, first upload the file with the Move_uploaded_file function, if it fails, upload the file with the copy function, upload to the specified directory and modify the directory properties.

Use this code to have file upload permission, but also to define the $path upload path, in addition, must have uploaded content, used alone, no output content, the code is as follows:

if (function_exists (' move_uploaded_file ') && move_uploaded_file ($attachment, $path))

{

chmod ($path, 0666); Changing the file access mode

$attachment = $path;

}

ElseIf (Copy ($attachment, $path))//If move_upload_file use Cope ()

{

chmod ($path, 0666); Changing file access Properties

$attachment = $path;

}//Open Source Code phpfensi.com

  • Related Article

    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.