Home > Backend Development > PHP Tutorial > Common file operations in php notes_PHP tutorial

Common file operations in php notes_PHP tutorial

WBOY
Release: 2016-07-21 15:33:48
Original
869 people have browsed it

Copy code The code is as follows:

//Common file operation functions

// The first part is file reading, writing, creation, deletion, renaming, etc.
//Before operating the file, let’s first determine whether it is a file and whether it is executable, readable and writable
$file="test.txt";
if(file_exists($file))//Whether the disk file exists
{
echo "The file exists
";
}else
{
echo "The file does not Exists, created";
$fp=fopen($file,"w");//Read-only mode creation
fclose($fp);
}
if(is_file($file ))
{
echo "is a file
";
}
if(is_dir($file))
{
echo "is a directory
" ;
}

if(is_executable($file))
{
echo "File executable
";
}
if(is_readable($file ))
{
echo "The file is readable
";
}
if(is_writable($file))
{
echo "The file is writable}
chmod($file,0777);//Full permissions
//Mode Description The number 1 means to make the file executable, the number 2 means to make the file writable, and the number 4 means to make the file Readable - the addition of modes represents permissions
$fp=fopen("test.txt","a+");//Open with append read and write methods
//When opening a remote file
/ /$fp=fopen("test.txt","a+b"); Remember to add b;
$content=fread($fp,70);//Read 70 bytes
echo "1 .{$content}
";//Output
fwrite($fp,"I amRecommendation$content=file_get_contents("test.txt");//Reading files It is recommended to use this function to read remote files
//$content=file_get_contents( "http://www.jianlila.com");
echo "2.{$content}
";
file_put_contents("test.txt","I am
Love My Parentsasddddddddddddddddddddddddddddxxxxxxxxx");
//Output to file
fclose($fp);//Close the file handle
$fp=fopen("test.txt","a+");
$content=fread($fp,filesize("test.txt"));
//Read all content filesize($file )//Number of file bytes
echo "3.{$content}
";
$fp=fopen("test.txt","r");
echo "One character ".fgetc($fp)."
";//Read one character
$fp=fopen("test.txt","r");
echo "One line".fgets( $fp)."
";//Read a line of characters
$fp=fopen("test.txt","r");
echo "remaining data";
fpassthru( $fp);
echo "
";//The remaining data can be used to output binary files
copy("test.txt","Jianlila.txt");
/ /File copy
if(file_exists("Love My Parents.txt"))
{
unlink("Love My Parents.txt");
//Delete the file if it exists
}
rename("Recommendation gift.txt","Love my parents.txt");
//File rename

if(file_exists("Recommendation gift") )
{
rmdir("Recommended gift");//Delete folder
}else
{
mkdir("Recommended gift");//Create folder
}


//Get file information function
$file="test.txt";
echo "File size".filesize($file)."bytesecho "File type".filetype($file)."
";
//The file type here is not the .txt we see, which refers to fifo, char, dir, block, link, file and unknown
$fp=fopen($file,"r");//Open the file
print_r(fstat($fp));//Print file information
echo "Current file path information".__FILE__."
";
echo "The directory where the current file is located".dirname(__FILE__)."
";
echo "Current file name". basename(__FILE__)."
";
print_r(stat($file));//Print file information

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322562.htmlTechArticleCopy the code code as follows: ?php //Commonly used file operation functions//The first part of file reading, writing, creation and deletion Naming, etc.//Before we start operating the file, let’s first determine whether it is a file...
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