简体   繁体   English

仅在文件路径具有特定扩展名时显示数组行

[英]Display array rows only if a file path has a certain extension

I have an array, which contains amongst other things a file path to a video file. 我有一个数组,其中除其他外还包含视频文件的文件路径。 There are various file formats .avi, .mp4 etc etc. 有各种文件格式.avi,.mp4等。

Now I only want to display the rows where the file path's file extension is .mp4. 现在,我只想显示文件路径的文件扩展名为.mp4的行。

Currently I'm using a function to extract the file extension, but I'm having trouble doing this on the fly while displaying the rows. 目前,我正在使用提取文件扩展名的功能,但是在显示行时却无法立即执行此操作。

An example of the file path: c:\\TempVideoDir\\Office-P-cam01-20140826-083016.avi 文件路径的示例:c:\\ TempVideoDir \\ Office-P-cam01-20140826-083016.avi

The function im using to get the file ext: im用于获取文件ext的函数:

function get_fileext($filename) {

   $cut_fileext = (explode('.', $filename));
   $just_fileext = $cut_fileext[1];

   echo $just_fileext; 
}

Which works fine. 哪个工作正常。

Now I'm querying the database to get the filepath along with other details about the file, and I only want to display the rows where the file path contains a .mp4 file. 现在,我正在查询数据库以获取文件路径以及有关该文件的其他详细信息,而我只想显示文件路径包含.mp4文件的行。

I've tried everything I could thing of/find on Google etc but this is what I currently have: 我已经尝试了所有可以在Google等上找到的东西,但这是我目前拥有的:

  $stmt->execute();
  $result = $stmt->fetchAll();

  foreach($result as $row) {

        if (in_array((get_fileext($row["Filename"])== "mp4"), $result)) {

        echo "  blah blah

I dont have the option of adding the file extension into the database table as this is backed up automatically by a piece of equipment. 我没有选择将文件扩展名添加到数据库表中的选项,因为这是由设备自动备份的。

So, whadoyareckon? 那么,wadoyareckon? is it possible to do it on the fly like this? 这样可以飞起来吗?

Any help is much appreciated! 任何帮助深表感谢!

Thanks 谢谢

I dont think you can use in_array() like that. 我不认为您可以使用in_array()。 Its searching the entire index of the array, so when it looks ate $result[] its never going to find $row['filename'] with only an ext. 它搜索数组的整个索引,因此当它看起来像$ result []时,它永远不会只用ext查找$ row ['filename']。

try this: 尝试这个:

//looping through $result array
foreach($result as $row) {

    //check if $row['filename'] has a string with a mp4 extension.
    if (get_fileext($row["Filename"])== "mp4") {

    echo "Row has a filename with the mp4 extension';

also as someone else pointed out fix your get_fileext() method to return something. 也正如其他人指出的那样,修复您的get_fileext()方法以返回某些内容。

function get_fileext($filename) {

   $cut_fileext = (explode('.', $filename));
   $just_fileext = $cut_fileext[1];

   return $just_fileext; 
}

Your function isn't returning a value. 您的函数没有返回值。 It's emitting the value to the output: 它向输出发出值:

function get_fileext($filename) {

    $cut_fileext = (explode('.', $filename));
    $just_fileext = $cut_fileext[1];

    echo $just_fileext; 
}

So there's no way for anything invoking that function to see that result. 因此,任何调用该函数的方法都无法看到结果。 Instead, return the value: 而是返回值:

function get_fileext($filename) {

    $cut_fileext = (explode('.', $filename));
    $just_fileext = $cut_fileext[1];

    return $just_fileext; 
}

That way the invocation of the function itself evaluates to the returned value, allowing you to use the function in your logic. 这样,对函数本身的调用将评估为返回的值,从而使您可以在逻辑中使用该函数。

You could consider adjusting your database query to only return rows with ".mp4" files. 您可以考虑将数据库查询调整为仅返回带有“ .mp4”文件的行。

Change your query to have something like: 将查询更改为类似以下内容:

filepath = "%.mp4"

In MySQL the % operator is a wildcard, so this will match any strings ending in ".mp4" 在MySQL中, %运算符是通配符,因此它将匹配以“ .mp4”结尾的所有字符串

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

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