Home > Backend Development > PHP Tutorial > Detailed explanation of example code for deleting read files and obtaining file images in PHP

Detailed explanation of example code for deleting read files and obtaining file images in PHP

伊谢尔伦
Release: 2023-03-14 08:54:01
Original
1279 people have browsed it

1. Delete the file

unlink()

Syntax: int unlink(string filename);

Return value: integer

Function type: file access. For example:

unlink("tmp/test.txt");
Copy after login

PHP reads the file by lineRemove the newline character "\n":

$content=str_replace("\n","",$content);
echo $content;

$content=str_replace(array("\n","\r"),"",$content);

$content=preg_replace("/\s/","",$content);

$content=trim($content);
Copy after login

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);
  }
}
Copy after login

3. Read the picture name under the folder

<?php
$handle = opendir(&#39;images/&#39;); //当前目录
  while (false !== ($file = readdir($handle))) { //遍历该php文件所在目录
   list($filesname,$kzm)=explode(".",$file);//获取扩展名
    if($kzm=="gif" or $kzm=="jpg" or $kzm=="JPG") { //文件过滤
     if (!is_dir(&#39;./&#39;.$file)) { //文件夹过滤
      $array[]=$file;//把符合条件的文件名存入数组
      $i++;//记录图片总张数
      }
     }
  }
 print_r($array);
?>
Copy after login

The above is the detailed content of Detailed explanation of example code for deleting read files and obtaining file images in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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