-1

I have image filename being returned from a MYSQL database:

1073-6431.jpg

I need to return

1073-6431.webp

to access the webp version of the file - there are two files with same name, different extensions.

Simple right? For some reason nothing works though!

I've tried to make an array:

$file_array = explode('.', $file_name);
echo $file_array[0].'.webp';

I've tried to use pathinfo:

$fileName = pathinfo($file_name )['filename'];
echo $fileName. '.webp';

All I get is '.webp'

BUT if I explicitly enter the $file_name value:

$file_name = '1073-6431.jpg';

IT WORKS FINE! It just doesn't like using the value directly from the database. The database value is text and not an integer. I even tried to convert the value to text just make sure. Does anyone have any idea what I might be doing wrong?

roboron
  • 1
  • 3
  • 2
    The described behavior suggests that `$file_name` probably isn't what you think it is. Can you elaborate on how that value is being set? Can you log or output that value during debugging to observe it? – David Aug 31 '23 at 22:49
  • 2
    What does `var_dump(pathinfo($file_name))` show? – Barmar Aug 31 '23 at 22:53
  • 1
    Remember filenames can and will have `.` in them, like `img.1.2.3.4.png`. `pathinfo()` as Barmar suggests, is on the right track here. – tadman Aug 31 '23 at 22:54
  • Argh! Found the error. Put the fetch() AFTER the script. So stupid. Sorry. – roboron Aug 31 '23 at 23:38

0 Answers0