Previously we set up our Xforms to send some file names to our php file (see Directory List: Delete files )
Here’s the php to process them.
<?php
header("Content-type: application/xhtml+xml");
//header("Content-type: text/plain");
include("fnGeneric.php");
include("fnMisc.php");
if (!isset($HTTP_RAW_POST_DATA))
$HTTP_RAW_POST_DATA = file_get_contents("php://input");
//put the data into an associative array (see my Parsing without the DOM for this function)
$data=xmlSTR2xmlAssocARR($HTTP_RAW_POST_DATA);
/* an Xforms select control will return several space delimited values
for a single element like this:
"file.odt file2.odt file3.odt"
so what we end up with in our associative array is
deleteFiles=> "file.odt file2.odt file3.odt"
so we're goint to read this into a string variable and
then explode it into an array so we can work with the files individually.
Because we are in control of the design of this application,
we can make sure our filenames don't have spaces.
tip: If you are in a situation where the file name has spaces,
make sure you replace those spaces before your Xforms processes that data.
For example something like "file number two.odt" --using str_replace
can be changed to "file,,sp,,number,,sp,,two.odt"
then when you need to process data from a select control submission, you can
separate the files with an explode around the spaces.
Then when you need the real file name for processing, you
can replace each ,,sp,, with a space again,
then manipulate the files as you need.
If you don't do this, it'll be hard to separate the data later.*/
$delFiles=$data[deleteFiles];
$delFiles=explode(" ", $delFiles);
/*next is straightforward, use "unlink" to delete a file,
but first make sure the file exists so our program doesn't
shut down or send our unsuspecting user a nasty message*/
/*notice we'll delete the chosen file
and all it's associated files.*/
foreach ($delFiles as $value)
{
if ( file_exists("$value") )
unlink($value);
$value=str_replace(".xhtml","",$value);
if ( file_exists("$value.css") )
{ unlink("$value.css"); }
if ( file_exists("$value.xf.css") )
{ unlink("$value.xf.css"); }
if ( file_exists("$value.cr.css") )
{ unlink("$value.cr.css"); }
if ( file_exists("$value.Inst.xml") )
{ unlink("$value.Inst.xml"); }
if ( file_exists("$value.xfile.xml") )
{ unlink("$value.xfile.xml"); }
}
//the following page will be echoed when finished
$page=<<<STR
<?xml version="1.0"?>
<?xml-stylesheet href="Forms/zstyle.css" type="text/css"?>
<?xml-stylesheet href="Forms/zstylexf.css" type="text/css"?>
<?xml-stylesheet href="Forms/zstylecr.css" type="text/css"?>
<h:html
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:cr="http://www.cr.com/2007/markup">
<h:head>
<h:title>
Reload
</h:title>
<xf:model>
<xf:action ev:event="xforms-ready">
<xf:load resource="fileManager.php" show="replace"/>
</xf:action>
</xf:model>
</h:head>
<h:body>
reloading page.....
</h:body><h:p/> reloading page.....
</h:html>
STR;
echo $page;
?>