tags:

views:

251

answers:

4

Hello! I wan't to search a filename for example like this string:

$filetype = "file.php.jpg";

And then i want to search if it finds php in that string, then return false;

Now if a filename is named php-logo.jpg i don't want it to be denied. Maybe we should consider searching for .php. in the string?

+1  A: 

You might want to reconsider the return value. Instead of returning false if it has PHP, why not return true if it does? What about this?

function containsPhp($string) {

    return strstr($string, 'php');

}

Then you could do this

$filetype = "file.php.jpg";

// does it contain php in it?

var_dump(containsPhp($filetype));

UPDATE

If you know for certain it will always appear as '.php.', then simply change the second argument of the strstr() function.

If you want the file extension, consider the following function.

function getFileExtension($filePath) {

    $filename = basedir($filePath); // this may not be required.

    return pathinfo($filename, PATHINFO_EXTENSION);
}

}

alex
Yup, thank you, this post was helpful!
@Pete Glad to help :)
alex
A: 
$filetype = "file.php.jpg";
if ( substr_count($filetype, 'php') > 0 ){

} else {
    return false;
}

substr_count

If you want check only the extension:

function get_file_extension($file_name)
{
    return substr(strrchr($file_name,'.'),1);
}
if ('php' == get_file_extension($filetype)) {

} else {
    return false;
}
erenon
Your first example, if i put a dot in front of php would that work?
Yes, it would.
erenon
A: 

What if one sends php-logo.png?

Aif
Good point - Hopefully the OP can modify one of the other solutions to incorporate this.
alex
Ah, yes that's quite a question! Then it would be denied by the examples below. Hm..
the question is: "search if it finds php in that string"
erenon
A: 

Why do you complicate it so much? :)

    $file = 'some.php.file';
    if (false !== strpos($file,'.php')) {
       // file contains .php
       return false;
    }
    else {
      // file not php
    }
bisko