Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Norbertho's avatar

basename problem

Hi, Is it possible that basename function does not wok correctly i have the following code and it always gives back the full path for me not just only the file name.

    $origiimg = ($teams->profileimg);
    $new = basename($origiimg);
    dd($new );

dd it returns "C:\fakepath\alps.jpg"

then i have tried

      $origiimg = "C:\fakepath\alps.jpg";
      $new = basename($origiimg);
     dd($new );

dd it returns "C:\fakepath\alps.jpg"

Do i miss something here? It should be very simple to get "alps.jpg" as far as I know but no luck for 5hours now.

0 likes
2 replies
Cronix's avatar
Cronix
Best Answer
Level 67

The problem is you're using windows style paths. If you switch your \'s to /'s it will work.

$origiimg = "C:/fakepath/alps.jpg";
$new = basename($origiimg);
dd($new ); // alps.jpg

basename() only works on /'s.

So if you have those paths hardcoded, like in the db or something (I hope not as they're specific to your OS), you could do something like

$origiimg = str_replace('\\', '/', $teams->profileimg);
$new = basename($origiimg);
dd($new );

It needs 2 \'s for the first part because \ is an escape char.

1 like

Please or to participate in this conversation.