简体   繁体   English

通过php中的scandir函数在html文件中以透视图类型显示文件

[英]Showing files in their perspective types in an html file through scandir function in php

I am trying to show all images in a directory through scandir function but I am getting just the first letter of every file name instead of full name to show a picture as : 我试图通过scandir函数显示目录中的所有图像,但是我只得到每个文件名的第一个字母而不是全名,以将图片显示为:

<?php
$path    = 'images/';
$files = scandir($path);
foreach ($files as $file) {
$x = 0;
$x_new = $x++;
echo "<img src='images/".$file[$x_new]."' >";
} 
?>

Here I am getting the result as : 在这里,我得到的结果是:

<img src='images/.' >
<img src='images/.' >
<img src='images/J' >
<img src='images/J' >
<img src='images/i' >

doing a var dump gives me this as : 做一个var dump给我这是:

array(5) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(17) "Job Board New.png" [3]=> string(13) "Job Board.png" [4]=> string(9) "index.jpg" }

Plus there are two . 再加上有两个. and .. elements in the array I don't have any files in the directory with these names so what these are really? ..数组中的元素我在目录中没有这些名称的文件,那么这些到底是什么?

Will be great if you people please guide me in the right way..! 如果您能以正确的方式指导我,那将是很棒的..!

Plus How can I get the extension of files and then do different things using functions..! 加上如何获取文件扩展名,然后使用函数执行其他操作。

Use trim() to remove . 使用trim()删除. , also use $file instead of $file[$x_new] . ,也请使用$file代替$file[$x_new] Example here... 这里的例子...

foreach ($files as $file) {
    $name = trim($file, ' .');
    if($name){
        echo "<img src='images/".$name."' >";
    }
}

The '.' “。” directory means the current directory. directory表示当前目录。 The '..' directory means the parent directory of the current directory. “ ..”目录表示当前目录的父目录。 This is common under UNIX, LINUX and Windows. 这在UNIX,LINUX和Windows下很常见。

Use pathinfo to read the info about the file and its types. 使用pathinfo读取有关文件及其类型的信息。 The array index extension gives the extension of the file. 数组索引扩展名提供文件的扩展名。

$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM