1

I have a table called 'filename'. I try to output <a> tags in a loop from it like this:

<?php

while($sermon = mysql_fetch_assoc($sermonsQ)) {
    echo '<a href="admin/'. $sermon ['filename'] . '">';
    echo 'download</a></td>';
}

Current problem is, that $sermon['filename'] containts a leading path like path/test.mp3. But I need only the filename without the path, like test.mp3. How can I do this?

2

3 Answers 3

5

Use basename() for that. It will return the filename without the leading path:

basename($sermon ['filename'])
Sign up to request clarification or add additional context in comments.

1 Comment

:) np. You should consider to improve the code example in the question to something that is at least valid PHP code. Maybe it will get up-voted then ;)
0

You can use path_info()

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?>

also mysql_* functions are depracated so you shouldn't use them for example to PDO or mysqli

Comments

0
<?php

while($sermon = mysql_fetch_assoc($sermonsQ)) {
    $filename = explode('/',$sermon ['filename']);
    echo '<a href="admin/'. $filename[1] . '">';
    echo 'download</a></td>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.