Php 我想使用文件类型显示文件列表
我想将文件显示为文件类型。如果文件是图像显示这是图像,如果文件是PDF显示文件是PDFPhp 我想使用文件类型显示文件列表,php,file-type,Php,File Type,我想将文件显示为文件类型。如果文件是图像显示这是图像,如果文件是PDF显示文件是PDF 请帮我输入这个代码。Pls,Pls您可以执行一个简单的str_pos()/substr()来检测字符串是否包含.pdf文件扩展名或.html或.png/.jpg 下面是一些示例代码: <?php //scan "uploads" folder and display them accordingly $scan = scandir('uploads');
请帮我输入这个代码。Pls,Pls您可以执行一个简单的
str_pos()
/substr()
来检测字符串是否包含.pdf文件扩展名或.html或.png/.jpg
下面是一些示例代码:
<?php
//scan "uploads" folder and display them accordingly
$scan = scandir('uploads');
foreach($scan as $file)
{
if (is_image("uploads/$file"))
{ ?>
<p><?php echo "this is an image"; ?></p>
<?php
}
if (is_pdf("uploads/$file"))
{ ?>
<p><?php echo "this is an PDF file"; ?></p>
<?php
}
if (is_html("uploads/$file"))
{ ?>
<p><?php echo "this is an HTML file"; ?></p>
<?php
}
}
?>
只需为不同的文件类型进行其他切换,您就获得了成功。理想情况下,将所有文件传递给一个函数,并在其中包含所有逻辑。您不必依赖文件扩展名:
if (substr($filename, -4) == '.jpg') {
echo "Jpeg!";
}
$fi = finfo_open(FILEINFO_MIME_TYPE);
foreach(scandir('uploads') as $file) {
echo finfo_file($fi, $file);
}
或获得分机:
if (substr($filename, -4) == '.jpg') {
echo "Jpeg!";
}
$fi = finfo_open(FILEINFO_MIME_TYPE);
foreach(scandir('uploads') as $file) {
echo finfo_file($fi, $file);
}
我会使用这样的方法:
echo pathinfo($file, PATHINFO_EXTENSION);
阅读有关使用此项的更多信息:
$ext=pathinfo($path,pathinfo\u扩展名)代码>你也可以使用扩展名,你能告诉我为什么使用-4吗?是的,正如你在http://php.net/substr
--指定负数时,函数将从末尾开始向后读取,而不是从前面开始向前读取。它读取的字符数是参数的绝对值。因此.html
距离末尾5个字符。恕我直言,这可能是最糟糕的解决方案。没问题,很高兴我能帮上忙。