search
HomeBackend DevelopmentPHP TutorialPHP file operation function set

PHP file operation function set

Nov 29, 2016 pm 01:31 PM
php

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

Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.