PHP file operation function set

高洛峰
Release: 2016-11-29 13:31:02
Original
1182 people have browsed it

This article collects a large number of file operation functions in PHP, such as file opening, creation, deletion, group change, reading and writing files, file uploading, opening remote files, writing content to files, etc.

$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 reading and writing mode, point the file pointer to the file header

$fp=fopen("test.txt","w");

//Open the file in writing mode, point the file pointer to the file header and File size is truncated to zero. If the file does not exist, try to create it

$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 it

$fp=fopen("test.txt","a");

//Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it

$fp=fopen("test.txt","a+");

//Open in read-write mode and point the file pointer to the end of the file. If the file does not exist, try to create it

$fp=fopen("test.txt","wb+");

//Open the binary file in writing 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 it

$fp=fopen("c: est est.txt","r");

//On Windows platform, escape each backslash in the file path, Or use slashes

$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 in to the server with username and password

/*

This example code has no output

* /

$fp=fopen("test.txt","r");                                                Get the file content into the variable

print "$msg"; //Output the file content

fclose($fp); t" ,"r"); //Open the file in read-only mode

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

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

echo $read; It should be noted that if the command to be executed is not found, a legal resource will be returned. It allows access to any error information returned by the shell. When safe mode is turned on, only programs within safe_mode_exec_dir can be executed. The path pointing to the program cannot contain the .. component, that is, commands cannot be executed outside the specified directory.

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

echo $read; //Output content

pclose($fp);

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

{

echo "Clear cache below";

}

echo "

";

clearstatcache(); //Clear file status cache

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

{

die('cleared');

}

//

$filename="test.txt"; /Define 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 permissions, other users have read-only permissions

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

//The file owner has all permissions, and other users have read-only permissions and execution permissions

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

//The file owner has all permissions, and the user group to which the file owner belongs has read and execution permissions

//

$file="test.txt"; //Define file

delete($file);

The following code implements the file upload function. First, use the move_uploaded_file function to upload the file. If it fails, use the copy function to upload the file, upload it to the specified directory and modify the directory attributes.

To use this code, you must have file upload permissions and define $path Upload path, in addition there must be upload 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 ; ,0666); //Change file access attributes

$attachment=$path;

}//Open source code phpfensi.com

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!