1

i have this code that is listing all my mp3 links in the directory, but the audio player won't play any files that have spaces in the file names, if i remove the spaces, it works but i was wondering if i can some how have the script add %20 when there is a space in the file name and that way the audio player i am using can pickup on it

Thanks!

heres my code

    <ul id="playlist">
<?php
$dirFiles = array();
// opens images folder
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {

        // strips files extensions      
        $crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-", "error_log", ".php");    

        $newstring = str_replace($crap, " ", $file );   

        //asort($file, SORT_NUMERIC); - doesnt work :(

        // hides folders, writes out ul of images and thumbnails from two folders

        if ($file != "." && $file != ".." && $file != "index.php" && $file != ".DS_Store" && $file != "download.php" && $file != "error_log" && $file != "Thumbnails") {
            $dirFiles[] = $file;
        }
    }
    closedir($handle);
}

sort($dirFiles);
foreach($dirFiles as $file)
{
    //echo "<li><img style=\"padding-right: 10px;vertical-align: middle;height: 60px;\" src=\"http://www.ggcc.tv/LogoNE.png\" />";
    echo '<li><a href="'.$file.'">'.$file.'<br></li>';
}

?>
    </ul>
1
  • 1
    "+" will also work for spaces Commented Dec 13, 2014 at 23:03

3 Answers 3

3

try rawurlencode or other Url Functions

echo '<li><a href="'.rawurlencode($file).'">'.$file.'<br></li>';
Sign up to request clarification or add additional context in comments.

Comments

2

Use str_replace to replace spaces with %20.

$fileURL = str_replace(' ', '%20', $file);
echo '<li><a href="'.$fileURL.'">'.$file.'<br></li>';

Comments

0

Alternatively one may modify the foreach loop as follows:

foreach($dirFiles as $file)
{
    $url = join("%20",explode(' ',$file ) );
    echo '<li><a href="'.$url.'">'.$file.'<br></li>';
}

The first statement encodes only spaces and assigns the result for later use by the HREF attribute of the <a> tag. rawurlencode is another option to achieve a similar result, but it may also encode more than just space characters which at times is good and other times may cause undesirable effects, such as encoding a character that is part of a domain name. str_replace is probably the sleeker solution since it requires only one function call instead of of the two shown in this snippet.

The TIMTOWDI aspect of PHP is particularly evident with this question. You may also do the following:

<?php
$from = ' ';
$to = '+';

foreach($dirFiles as $file)
{
    $url = strtr( $file, $from, $to);
    echo '<li><a href="'.$url.'">'.$file.'<br></li>';
}

With this solution, the replacement character for the space is the simple '+' (PHP's way of encoding spaces) and it uses strtr() which some think is faster than str_replace(). Any '+' characters will revert to spaces after the the browser automatically decodes the url.

see simple example here

2 Comments

Your solution seems correct but URL needs to be: $url = strtr( $file, $from, $to);
Thank you for pointing out that the parameters for strtr() were incorrectly ordered. I've made the correction and added a link to example code that runs in virtually every PHP version.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.