Peter Bielak Peter Bielak - 5 months ago 30
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.

$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');

// 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
if(false == $di->isDot())
if($di->isDir() && true != in_array($di->getBasename(),$h_folders))
$fl_node = $file_list->createElement('dir');
}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');
$name = $file_list->createElement('name',$di->getBasename());
$path = substr_replace($di->getRealPath(), 'root', 0, strlen($root));
$path_node = $file_list->createElement('path', $path);
}else $di->next();

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

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


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().