PHP common file Operation function Summary _php skill

Source: Internet
Author: User
Tags access properties chmod fread php programming

This article has analyzed the PHP common file operation function. Share to everyone for your reference. The specific methods are as follows:

There are a lot of PHP file operation functions such as file Open, create, delete, accounts group, read write file, file upload and open remote files, write content to file and other instances.

Copy Code code as follows:
$FP =fopen ("Test.txt", "R");
Open the file as read-only and point the file pointer to the file header
$FP =fopen ("Test.txt", "r+");
Open the file read-write and point the file pointer to the file header
$FP =fopen ("Test.txt", "w");
Write to open, point the file pointer to the file header and truncate the file size to zero. Attempt to create if the file does not exist
$FP =fopen ("Test.txt", "w+");
Opens read-write, points the file pointer to the file header, and truncates the file size to zero. Attempt to create if the file does not exist
$FP =fopen ("Test.txt", "a");
Opens as write, pointing the file pointer to the end of the file. Attempt to create if the file does not exist
$FP =fopen ("Test.txt", "A +");
Opens in read-write mode, pointing the file pointer to the end of the file. Attempt to create if the file does not exist
$FP =fopen ("Test.txt", "wb+");
Opens the binary file as write, points the file pointer to the file header, and truncates the file size to zero. Attempt to create if the file does not exist
$FP =fopen ("C:est est.txt", "R");
On the Windows platform, you want to escape each backslash in the file path, or use a slash
$FP =fopen ("http://www.domain.com/", "R");
Open a 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
/*
This instance code has no output results
*/

$FP =fopen ("Test.txt", "R"); Open File
$msg =fread ($fp, FileSize ("test.txt")); Read the contents of a file to a 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
This executes the command with Popen, which is a post-submitted parameter and returns the execution result. Note that if the command to be executed is not found, 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. Composition, that is, the order cannot be executed outside the specified name.

$fp =popen ($_post[' command '], ' r '); Open process File
$read =fread ($fp, 2096); Reading process file pointers to variables
Echo $read; Output content
Pclose ($FP); Close Process File

$file = "Test.txt"; Definition file
if (file_exists ($file))//Determine if the file exists
{
echo "Clear cache below";
}
echo "<p>";
Clearstatcache (); Purge file state cache
if (file_exists ($file))//Determine if the file exists
{
Die (' clear Complete ');
}

$filename = "Test.txt"; Definition file
$user = "admin"; Define User
Chgrp ($filename, $group); Change the owner of 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 access, and other users have read-only access
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 the permissions, and the user group with the file owner has read and execute permissions

$file = "Test.txt"; Definition file
Delete ($file); deleting files
In PHP programming, you usually use the unlink function to delete files.

The following code to achieve file upload function, first with the Move_uploaded_file function upload file, if the failure of the copy function to upload files, upload to the specified directory and modify the directory properties.

Use this code to have file upload permissions, but also to define $path upload path, there must be upload content, separate use, no output content, the code is as follows:

Copy Code code as follows:
if (function_exists (' move_uploaded_file ') && move_uploaded_file ($attachment, $path))
{
chmod ($path, 0666); changing file access mode
$attachment = $path;
}
ElseIf (Copy ($attachment, $path))//If Move_upload_file is cope ()
{
chmod ($path, 0666); Changing file access Properties
$attachment = $path;
}

I hope this article will help you with your PHP program design.

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.