如何使用PHP获取特定文件夹中的所有图片和视频?(How to get all pictures and videos from a specific folder with php?)

编程入门 行业动态 更新时间:2024-10-28 06:31:06
如何使用PHP获取特定文件夹中的所有图片和视频?(How to get all pictures and videos from a specific folder with php?)

我正在尝试使用以下代码:它似乎可以获取图像而不是视频。 也许PATHINFO_EXTENSION无法与字符串进行比较?

<?php $files = glob("MyFolder/*.*"); for ($i = 0;$i < count($files);$i++) { $image = $files[$i]; $supported_file = array( 'jpg', 'jpeg', 'png', 'mp4', ); $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION)); if (in_array($ext, $supported_file)) { if (PATHINFO_EXTENSION == 'mp4') { echo '<video controls> <source src="' . $image . '" type="video/mp4"/>'; echo '</video>'; } else { echo '<img src="' . $image . '" alt="Random image" />'; } } else { continue; } } ?>

I'm trying with following code: It seems to get the images but not the videos. Maybe the PATHINFO_EXTENSION can not be compared to string?

<?php $files = glob("MyFolder/*.*"); for ($i = 0;$i < count($files);$i++) { $image = $files[$i]; $supported_file = array( 'jpg', 'jpeg', 'png', 'mp4', ); $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION)); if (in_array($ext, $supported_file)) { if (PATHINFO_EXTENSION == 'mp4') { echo '<video controls> <source src="' . $image . '" type="video/mp4"/>'; echo '</video>'; } else { echo '<img src="' . $image . '" alt="Random image" />'; } } else { continue; } } ?>

最满意答案

您的线路上出现错误,检查“mp4”的扩展名:

如果(PATHINFO_EXTENSION == 'MP4')

您应该与$ext变量的值进行比较,如下所示:

if($ext == 'mp4')

PATHINFO_EXTENSION不是你的变量,它只是pathinfo()函数的一个参数,它告诉它返回给定路径的文件扩展名。

PHP Docs - pathinfo: http : //php.net/manual/en/function.pathinfo.php

调用pathinfo()的返回值存储在变量$ext ,您需要将其与之进行比较。

You've got an error on your line that checks the extension against 'mp4':

if(PATHINFO_EXTENSION=='mp4')

You should be comparing against the value of your $ext variable, like so:

if($ext == 'mp4')

PATHINFO_EXTENSION is not your variable, it's just a parameter to the pathinfo() function which tells it to return the file extension for a given path.

PHP Docs - pathinfo: http://php.net/manual/en/function.pathinfo.php

The return value from the call to pathinfo() is stored in your variable $ext, and it's this that you need to compare against.

更多推荐

本文发布于:2023-04-27 15:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件   夹中   图片   视频

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!