Jump to content

[SOLVED] Getting PHP to ignore the ' symbol


eddy556

Recommended Posts

I have set up a PHP script to display the contents of certain folders on my server and create a link for each one so they can be downloaded.  The problem is that some filenames have the character ' in and so this causes havoc and causes it to not run correctly.  Here is the code i am using:

 

<?php

 

 

$directory = "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Media\B";

$path = "Media/B/";

 

if ($handle = opendir($directory))

{

while (false !== ($file = readdir($handle)))

 

{

if ($file != "." && $file != "..")

{

 

$test = substr($file, -4);

 

if ($test == ".mp3")

{

 

$filename = $path . $file;

 

echo "<a class='body' href='$filename'>$file</a></br>";

 

}

 

}

 

 

}

closedir($handle);

}

 

?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/54372-solved-getting-php-to-ignore-the-symbol/
Share on other sites

Okay thanks....I have now added the addslashes() method but it still seems to stop when it reaches a '.  I have tested the outcome and it has successfully got a / before every ' but it still seems to cut it off.  My new code is here:

 

<?php

 

 

$directory = "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Media\A";

$path = "Media/A/";

 

if ($handle = opendir($directory))

{

while (false !== ($file = readdir($handle)))

 

{

if ($file != "." && $file != "..")

{

 

$test = substr($file, -4);

 

if ($test == ".mp3")

{

 

$_filename = addslashes($file);

 

$filename = $path . $_filename;

 

 

 

echo "<a class='body' href='$filename'>$file</a></br>";

 

}

 

}

 

 

 

 

 

 

 

}

closedir($handle);

}

 

?>

 

 

Thanks for your help :-)

The addslashes() function is only useful for doing stuff withing PHP, it doesn't do anything for displaying HTML. For that you need to use the function htmlentities() with the ENT_QUOTES option.

 

<?php
$directory = "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/Media/A";
$path = "Media/A/";

if ($handle = opendir($directory))
{
   while (false !== ($file = readdir($handle)))
   {
         if ($file != "." && $file != "..")
         {
            $test = substr($file, -4);
               if ($test == ".mp3")
               {   
               $filename = $path . $file;
               echo '<a class="body" href="' . htmlentities($filename,ENT_QUOTES) . '">' . htmlentities($file,ENTQUOTES) . "</a></br>";
               }
         }
}
closedir($handle);
}
?>

 

BTW, always enclose all code snippets in


tags.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.