-1

I have a string of text that is the name of a file, and I want to show it only if the name of the file does not contain an image extension.

This are the string structures:

this-file-1.jpg
other_file.pdf
and-yet_another--file.zip
and-another-one.png

I've tried:

if ( strpos($value, 'jpg') === FALSE) {
    echo $mostrarArchivos;
}

And it does work, but when I try this it doesn't:

if ( strpos($value, 'jpg') === FALSE ||
    strpos($value, 'jpeg') === FALSE ||
    strpos($value, 'png') === FALSE ||
    strpos($value, 'gif') === FALSE
) {
    echo $mostrarArchivos;
}

In the sense that it does show the string with the filename, even if the string says whatever.jpg

I do realizse that this should be done differently, so any suggestion is more than welcome.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Rosamunda
  • 14,620
  • 10
  • 40
  • 70
  • 1
    You want to use `&&` (and), not `||` (or). There are [far more solid ways to get a file's extension](https://stackoverflow.com/questions/173868/how-to-getextract-a-file-extension-in-php), btw. Yours will fail if "jpg" or another one is present in the actual name. – Jeto Nov 30 '18 at 15:18
  • I assume your string will contain only one filename, is that correct? – RiggsFolly Nov 30 '18 at 15:30
  • I would like to know why I've been downvoted. I would like to know in order to improve my question next time. – Rosamunda Nov 30 '18 at 15:39

1 Answers1

2

This seems like a much simpler and more flexible solution, as you can add or remove extension easily to the $invalid array any time you like. This will also make sure you are testing the actual extension as well rather than a character sequence in the filename part that happens to match the extensions you want to ignore.

$invalid = ['jpg','gif','png'];

$filename = 'hello_world.png';

if ( ! in_array(pathinfo($filename, PATHINFO_EXTENSION), $invalid) ) {
    echo 'VALID EXTN ' . $filename . PHP_EOL;
} else {
    echo 'INVALID EXTN '  . $filename . PHP_EOL;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149