我正在尝试使用以下代码:它似乎可以获取图像而不是视频。 也许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.
更多推荐
发布评论