Filenames with single-quotes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gsmith411
Forum Newbie
Posts: 4
Joined: Fri Nov 05, 2010 4:01 pm

Filenames with single-quotes

Post 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>";
}

?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Filenames with single-quotes

Post 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>";
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Filenames with single-quotes

Post by AbraCadaver »

Try:

Code: Select all

echo '<li><a href="' . "$folder/$item" . '">' . $item . '</a></li>';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Filenames with single-quotes

Post 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>";
gsmith411
Forum Newbie
Posts: 4
Joined: Fri Nov 05, 2010 4:01 pm

Re: Filenames with single-quotes

Post by gsmith411 »

Thanx all for the help. I used Jonah's and it worked perfectly.
Post Reply