Page 1 of 1

Filenames with single-quotes

Posted: Fri Nov 05, 2010 4:05 pm
by gsmith411
I have the below code that lists all file names in a directory, sorted in alphabetical order. It works great, until a filename has an apostrophy in it, then it breaks. I tried urlencoding the filenames, but then the href can't fine that filename when I click on it.
HELP!! and thank you

Code: Select all

<?php

function alpharead3($dir){
if(!$dir){$dir = '.';}
foreach (glob("$dir/*") as $item){$sort[]= end(explode('/',$item));}

$killit = array('index.html', 'index.php', 'thumbs.db', 'styles.css');
$killcounter = 0;
foreach($sort as $sorteditem){
foreach($killit as $killcheck){
if(strtolower($sorteditem) == strtolower($killcheck))
{unset($sort[$killcounter]);}
}$killcounter++;}
if($sort){natsort($sort);}
foreach($sort as $item){$return[]= $item;}

if(!$return){return array();}
return $return;
}

//some basic usage

$folder = 'some_directory';
foreach(alpharead3($folder) as $item)
{

echo "<li><a href='" . $folder . "/" . $item . "'>"  . $item . "</a></li>";
}

?>

Re: Filenames with single-quotes

Posted: Fri Nov 05, 2010 4:16 pm
by Jonah Bron
Change

Code: Select all

echo "<li><a href='" . $folder . "/" . $item . "'>" . $item . "</a></li>";
To

Code: Select all

echo '<li><a href="' . $folder . '/' . $item . '">' . $item . '</a></li>';
Just use single quotes to surround the string, then the HTML tag is free to use double quotes. The alternative is to escape the double-quotes:

Code: Select all

echo "<li><a href=\"" . $folder . "/" . $item . "\">" . $item . "</a></li>";

Re: Filenames with single-quotes

Posted: Fri Nov 05, 2010 4:17 pm
by AbraCadaver
Try:

Code: Select all

echo '<li><a href="' . "$folder/$item" . '">' . $item . '</a></li>';

Re: Filenames with single-quotes

Posted: Fri Nov 05, 2010 4:45 pm
by Jonah Bron
AbraCadaver wrote:Try:

Code: Select all

echo '<li><a href="' . "$folder/$item" . '">' . $item . '</a></li>';
Or even

Code: Select all

echo "<li><a href=\"$folder/$item\">$item</a></li>";

Re: Filenames with single-quotes

Posted: Sat Nov 06, 2010 6:29 am
by gsmith411
Thanx all for the help. I used Jonah's and it worked perfectly.