Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.