This article analyzes the methods of php file operations with examples . Share it with everyone for your reference, the details are as follows:
1. Delete files
unlink()
Syntax: int unlink(string filename);
Return value: integer
Function type: file access. Such as:
unlink("tmp/test.txt");
2. Get the file name under the folder
$dir = "message/"; // 文件夹的名称 if (is_dir($dir)){ if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){ echo "文件名: $file <br>"; } closedir($dh); } }
3. Read the image names under the folder
<?php $handle = opendir('images/'); //当前目录 while (false !== ($file = readdir($handle))) { //遍历该php文件所在目录 list($filesname,$kzm)=explode(".",$file);//获取扩展名 if($kzm=="gif" or $kzm=="jpg" or $kzm=="JPG") { //文件过滤 if (!is_dir('./'.$file)) { //文件夹过滤 $array[]=$file;//把符合条件的文件名存入数组 $i++;//记录图片总张数 } } } print_r($array); ?>
Readers who are interested in more PHP related content can check out the special topics of this site: "Summary of PHP File Operations", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.