0

Although a $_FILES array is not shown, this is intended for file upload, I have had problems with file uploading so I was not able to get the [type] part of the $_FILES array yet... but this is just a simple problem of why isn't this function working...

   <?php

    function getExtension() {
        // global $testFile;
        $extension = "./mp3/"; // also tried simply mp3 without the forwards slashes
        $testFile = "song.mp3";
        if(preg_match($extension, $testFile)) {
        echo "match found";
        }else {
        echo "no match found";
        }

    }
    getExtension();

    ?>
janicehoplin
  • 397
  • 7
  • 15
  • 3
    Alternatively: [How to get the file extension in PHP?](http://stackoverflow.com/q/10368217) – mario Mar 10 '16 at 22:05
  • @mario Even better... – dmgig Mar 10 '16 at 22:05
  • @mario thanks for that, that is relevant, however I'm still wondering why that doesn't match above. Also I've stated that I haven't gotten the upload feature to work yet, from my undrestanding the file has to be uploaded to show the [type][size] parts... I mean when I dump the upload, it just says 0 for [size] and blank for [type] – janicehoplin Mar 10 '16 at 22:08
  • 1
    @janicehoplin It is because it is an invalid regular expression. They have to start and end with the same character, and `.` is a special character that needs to be escaped. Because the string always occurs at the end of the filename the regex should also be terminated using `$`. So, `./mp3/` should be `/\.mp3$/`. – Sverri M. Olsen Mar 10 '16 at 22:10
  • @Sverri, thanks for the clarification – janicehoplin Mar 10 '16 at 22:21

3 Answers3

5

Do this:

 $extension = "/mp3$/";

or

$extension = "/\.mp3$/";

ONLINE EXAMPLE

Reason being, you need the delimiters to begin and end your expression with (those are the slashes, though the can be any character) and can't have anything ahead of or behind them. The "$" will mean, find the string between the delimiters at the end of the string.

You can keep the period by escaping it (or else it will mean any character once, meaning testmp3 would be a match).

But really the best answer is as suggested in the comments - php's pathinfo() - since you are parsing a filename. Though in certain cases, you may want to do other tests for security, like check the mimetype.

Mr Rubix
  • 1,492
  • 14
  • 27
dmgig
  • 4,400
  • 5
  • 36
  • 47
  • thanks, curious what happened to the period? And if mp3 occurs more than once/case sensitive. I saw the \i for case sensitive, I'm not trying to set case sensitivity as the file name could have .MP3 I think – janicehoplin Mar 10 '16 at 22:09
  • The period won't work outside of the delimiters in any case, though you could have it there by escaping it (added to answer). – dmgig Mar 10 '16 at 22:11
  • 2
    If the filename can be both lowercase and uppercase then you can add `i` to the end of the regex (`/\.mp3$/i`). – Sverri M. Olsen Mar 10 '16 at 22:16
  • just a random thought, if it was an image, and for some reason it said file.mp3.jpg, and the .mp3 was picked up, then I would validate with mime? Or is there a way to easily distinguish... I saw something about using gd_image size or something like that. – janicehoplin Mar 10 '16 at 22:19
  • Well, if its name was `file.mp3.jpg` it wouldn't pass because the "$" enforces that the last 3 letters must be `mp3`. I have to say, using mimetype has failed a lot for me for mp3s, but using imagesize sounds like a very interesting option for detecting an image. If someone did just put an .mp3 on the end of an image then that's a different issue and there are all sorts of opinions on the best way to check: http://stackoverflow.com/questions/310714/how-to-check-file-types-of-uploaded-files-in-php – dmgig Mar 10 '16 at 22:20
  • The main thing is, you just don't want to execute dangerous files obviously, and just checking the extension alone isn't very secure. And will depend a lot on how you play the media - obviously a neat js player would get an image as input and presumably fail without much danger. – dmgig Mar 10 '16 at 22:26
4

If you want to keep the period, make sure you escape it.

$pattern = "/\.mp3$/";
georaldc
  • 1,880
  • 16
  • 24
  • I do want to keep the period however somewhere I read not to escape periods, can't find where I just saw that – janicehoplin Mar 10 '16 at 22:15
  • 1
    Dots have a special meaning in regular expressions (they match any character except line breaks). If you are trying to match a literal dot, then you have to escape it so that it isn't treated as a special character – georaldc Mar 10 '16 at 22:18
3

There is a mistake in your Regular Expression :

"./mp3/" => "/\.mp3$/"

Result :

<?php

function getExtension() {
    // global $testFile;
    $extension = "/\.mp3$/"; // also tried simply mp3 without the forwards slashes
    $testFile = "song.mp3";
    if(preg_match($extension, $testFile)) {
    echo "match found";
    }else {
    echo "no match found";
    }

}
getExtension();

?>

ONLINE EXAMPLE

Mr Rubix
  • 1,492
  • 14
  • 27