0

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.

Now I only want to display the rows where the file path's file extension is .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

The function im using to get the file 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.

I've tried everything I could thing of/find on Google etc but this is what I currently have:

  $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? is it possible to do it on the fly like this?

Any help is much appreciated!

Thanks

frobak
  • 557
  • 2
  • 11
  • 30
  • 1
    Close. You don't need to use `in_array` and you should change your function to use `return` instead of `echo`. Also what if the file name has more than one `.` in it? You're not going to return the correct extension in that case – ElefantPhace Sep 18 '14 at 14:37
  • if your path has more then one dot '.' you'll get problems – niyou Sep 18 '14 at 14:38
  • and a better way to get the extension: http://stackoverflow.com/questions/10368217/how-to-get-the-file-extension-in-php – niyou Sep 18 '14 at 14:40
  • The filepaths are output by a video recorder so will always be the same format so no need to worry about multiple .'s – frobak Sep 18 '14 at 14:48
  • I've removed the in_array and changed function to return but no, still no worky – frobak Sep 18 '14 at 14:51

3 Answers3

1

I dont think you can use in_array() like that. 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.

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.

function get_fileext($filename) {

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

   return $just_fileext; 
}
DirtyRedz
  • 566
  • 5
  • 14
0

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.

David
  • 208,112
  • 36
  • 198
  • 279
-1

You could consider adjusting your database query to only return rows with ".mp4" files.

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"

bnx
  • 417
  • 5
  • 11