Peter Bielak Peter Bielak - 4 months ago 20
PHP Question

php Getting files list &(ampersand) character error

I have code for getting files list in php, but if file name contain & character it doesn't display that file.
Here's the code:

Ps. I'm not php programmer and I really don't know what is this error.
All help will be very appreciated
Thanks so much in advance.

<?php
include_once('config.inc.php');
$current_dir = 'root';
if(array_key_exists('directory',$_POST)) {
$current_dir = $_POST['directory'];
}

// Creating a new XML using DOMDocument
$file_list = new DOMDocument('1.0');
$xml_root = $file_list->createElement('filelist');
$xml_root = $file_list->appendChild($xml_root);

// Setting the 'currentPath' attribute of the XML
$current_path = $file_list->createAttribute('currentPath');
$current_path->appendChild($file_list->createTextNode($current_dir));
$xml_root->appendChild($current_path);

// Replacing the word 'root' with the real root path
$current_dir = substr_replace($current_dir, $root, 0, 4);

$di = new DirectoryIterator($current_dir);

// Creating the XML using DirectoryIterator
while($di->valid())
{
if(false == $di->isDot())
{
if($di->isDir() && true != in_array($di->getBasename(),$h_folders))
{
$fl_node = $file_list->createElement('dir');
$xml_root->appendChild($fl_node);
}else if($di->isFile() && true !== in_array($di->getBasename(),$h_files)
&& true !== in_array(get_ext($di->getBasename()),$h_types))
{
$fl_node = $file_list->createElement('file');
$xml_root->appendChild($fl_node);
}else
{
$di->next();
continue;
}
$name = $file_list->createElement('name',$di->getBasename());
$fl_node->appendChild($name);
$path = substr_replace($di->getRealPath(), 'root', 0, strlen($root));
$path_node = $file_list->createElement('path', $path);
$fl_node->appendChild($path_node);
$di->next();
}else $di->next();
}

function get_ext($filename)
{
$exp = '/^(.+)\./';
return preg_replace($exp,'',$filename);
}

// Returning the XML to Flash.
echo $file_list->saveXML();
?>

Answer

The & character is used in HTML to write entities.

If you want to display arbitrary text in HTML, you need to escape it by calling htmlentities().