eddy556 Posted June 6, 2007 Share Posted June 6, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/54372-solved-getting-php-to-ignore-the-symbol/ Share on other sites More sharing options...
sanfly Posted June 6, 2007 Share Posted June 6, 2007 addslashes() Quote Link to comment https://forums.phpfreaks.com/topic/54372-solved-getting-php-to-ignore-the-symbol/#findComment-268878 Share on other sites More sharing options...
eddy556 Posted June 8, 2007 Author Share Posted June 8, 2007 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/54372-solved-getting-php-to-ignore-the-symbol/#findComment-270995 Share on other sites More sharing options...
kenrbnsn Posted June 8, 2007 Share Posted June 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54372-solved-getting-php-to-ignore-the-symbol/#findComment-271001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.