bruine
bruine

Reputation: 647

Get filename only

I have table :

==============
|id| document|
==============
|1 | doc1.txt|
|2 | doc2.txt|
==============

I wanna get the document without the extension. here's my code :

$query = mysql_query("SELECT document FROM tb ") or die(mysql_error());
while ($row = mysql_fetch_array($query)) {

    $filename    = $row['document'];
    $title   = basename($filename);
}

But the rsult of $title still contain the extension. what's wrong? thank you

Upvotes: 0

Views: 70

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You are manipulating a string, not a filename!

In your code, replace:

$title = basename($filename);

With:

$title = substr($filename, 0, strrpos($filename, "."));

I didn't use pathinfo() because, you are manipulating a string, and not a filename.

Upvotes: 1

zerkms
zerkms

Reputation: 254916

$title   = pathinfo($filename, PATHINFO_FILENAME);

pathinfo() is more suitable in this case

Upvotes: 4

Related Questions