Common file operations for PHP notes

  • 2020-03-31 21:15:47
  • OfStack


<?php 
//Common file manipulation functions

//The first part of the file read and write and create delete rename
//Before we start working with the file, let's see if it's a file and if it's executable, readable, and writable
$file="test.txt"; 
if(file_exists($file))//Whether the disk file exists
{ 
echo " File exists <br>"; 
}else 
{ 
echo " The file does not exist. It has been created "; 
$fp=fopen($file,"w");//Read-only mode creation
fclose($fp); 
} 
if(is_file($file)) 
{ 
echo " Is the file <br>"; 
} 
if(is_dir($file)) 
{ 
echo " Is a directory <br>"; 
} 

if(is_executable($file)) 
{ 
echo " File executable <br>"; 
} 
if(is_readable($file)) 
{ 
echo " File can be read <br>"; 
} 
if(is_writable($file)) 
{ 
echo " File can be written <br>"; 
} 
chmod($file,0777);//Full permission
//Pattern description the number 1 to make the file executable, the number 2 to make the file writable, and the number 4 to make the file readable -- pattern addition represents permissions
$fp=fopen("test.txt","a+");//Open by appending read and write
//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}<br> ";//The output
fwrite($fp," I am a <a href='http://www.jianlila.com'> Commend ceremony! </a>asdddddddddddddddddddddddddddddddddxxxxxxxxx");//Append mode is written
$content=file_get_contents("test.txt");//Read files this function is recommended for reading remote files
//$content=file_get_contents("http://www.jianlila.com"); 
echo "2.{$content}<br> "; 
file_put_contents("test.txt"," I am a <a href='http://www.aiwobama.com'> Love my parents </a>asdddddddddddddddddddddddddddddddddxxxxxxxxx"); 
//Output to file
fclose($fp);//Close the file handle
$fp=fopen("test.txt","a+"); 
$content=fread($fp,filesize("test.txt")); 
// Read everything  filesize($file)// File bytes  
echo "3.{$content}<br>"; 
$fp=fopen("test.txt","r"); 
echo " One character at a time ".fgetc($fp)."<br>";//Read a character
$fp=fopen("test.txt","r"); 
echo " A line of ".fgets($fp)."<br>";//Read a line
$fp=fopen("test.txt","r"); 
echo " The remaining data "; 
fpassthru($fp); 
echo "<br>";//Output remaining data can be used to output binary files
copy("test.txt"," Commend ceremony! .txt"); 
//File copy
if(file_exists(" Love my parents .txt")) 
{ 
unlink(" Love my parents .txt"); 
//Delete the file if it exists
} 
rename(" Commend ceremony! .txt"," Love my parents .txt"); 
//File renaming

if(file_exists(" Commend ceremony! ")) 
{ 
rmdir(" Commend ceremony! ");//Delete folder
}else 
{ 
mkdir(" Commend ceremony! ");//Create folder
} 


//Gets the file information function
$file="test.txt"; 
echo " The file size ".filesize($file)." byte <br>"; 
echo " The file type ".filetype($file)."<br>"; 
//The file types here are not the ones we see. TXT and so on. Twenty means 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__."<br>"; 
echo " The directory where the current file resides ".dirname(__FILE__)."<br>"; 
echo " Current file name ".basename(__FILE__)."<br>"; 
print_r(stat($file));//Print file information

?>

Related articles: