So, I'm having an issue with zlib that I'm not sure how to fix. This is for a bug report submission system for my TI-Trek game. I have bugs stored in the actual download packages themselves, such that the server has a way of reading the bugs and the end user has a catalog of them too. Here's what I'm trying to do:
1) From a form, retrieve the $_POST[] variables "version", "short", and "misc". Version is a link to the zip file we want to add or modify. Short is a short title, describing the issue. Misc is a longer description of the issue.
2) Attempt to open the zip file specified by "version".
3) Read out the contents of the file 'bugs.xml' in that zip. If FALSE is returned (meaning file doesn't exist), create a new XML string. If a string is returned (file exists), load that string as XML.
4) Add a <bug> node to the XML and populate it with the rest of the form data.
5) Write the XML to bugs.xml file within the zip.
6) Close the zip object.
Here is the code for the HTML form. Note the PHP that generates the select. Some of the echo's in there are purely for debugging:
Code:
Now the PHP to handle the form (yes, the zlib module is installed):
Code:
The first time I run this script on a file, I get the text "We got it!" and "Creating New String" as well as the correct file name. Any subsequent run, we get the same result, but without the "Creating new string".
The ZIP file gets modified such that the bugs.xml file is created properly, so that's not the problem here. What happens to the directory structure is the problem.
Here is the contents of the ZIP before the script runs (once):
Code:
Here is the contents of the ZIP after the script runs (once):
Code:
Can anyone shed some light on why this is occurring and how to prevent it?
1) From a form, retrieve the $_POST[] variables "version", "short", and "misc". Version is a link to the zip file we want to add or modify. Short is a short title, describing the issue. Misc is a longer description of the issue.
2) Attempt to open the zip file specified by "version".
3) Read out the contents of the file 'bugs.xml' in that zip. If FALSE is returned (meaning file doesn't exist), create a new XML string. If a string is returned (file exists), load that string as XML.
4) Add a <bug> node to the XML and populate it with the rest of the form data.
5) Write the XML to bugs.xml file within the zip.
6) Close the zip object.
Here is the code for the HTML form. Note the PHP that generates the select. Some of the echo's in there are purely for debugging:
Code:
<form id="bugreporter" method="post" action="">
<div>Bug Reporting</div><br />
Select Version: <select name="version">
<?php
$types = array("alpha", "beta", "stable");
foreach($types as $t){
foreach(array_reverse(glob("/home/trek/www/downloads/" . $t . "/*.zip")) as $file){
echo "<option value=\"" . $file . "\">";
echo $t . " - ";
echo explode("_", basename($file, ".zip"))[1];
echo "</option>";
}
}
?>
</select><br />
<input type="text" name="short" placeholder="A Short Description" /><br />
<textarea name="desc" placeholder="Describe the bug in greater detail here.">
</textarea>
<input type="submit" name="sendbug" value="Submit Bug Report" />
</form>
Now the PHP to handle the form (yes, the zlib module is installed):
Code:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['sendbug'])){
if($_POST['short'] == "") {$errors[] = "Bug title not provided";}
if($_POST['desc'] == "") {$errors[] = "Description not provided";}
if(empty($errors)){
$fileToModify = 'bugs.xml';
$fileToOpen = $_POST['version'];
echo $fileToOpen;
if(!is_writable($fileToOpen)){echo "File not writeable!";}
$zip = new ZipArchive;
if ($zip->open($fileToOpen) === TRUE) {
echo "We got it!";
$oldData = $zip->getFromName($fileToModify);
if($oldData === false){
$string = <<<XML
<?xml version='1.0'?>
<bugs>
</bugs>
XML;
$xml = simplexml_load_string($string);
echo "Creating new string";
} else {
$xml = simplexml_load_string($oldData);
$zip->deleteName($fileToModify);
}
$newbug = $xml->addChild("bug");
$newbug->addChild("id", count($xml->bug));
$newbug->addChild("short", $_POST['short']);
$newbug->addChild("desc", $_POST['desc']);
$zip->addFromString ('bugs.xml', $xml->asXML());
if($zip->close() === false) echo "Failed. Likely permission problem.";
}
}
}
?>
The first time I run this script on a file, I get the text "We got it!" and "Creating New String" as well as the correct file name. Any subsequent run, we get the same result, but without the "Creating new string".
The ZIP file gets modified such that the bugs.xml file is created properly, so that's not the problem here. What happens to the directory structure is the problem.
Here is the contents of the ZIP before the script runs (once):
Code:
titrek_0.0.28.zip /
TITREK.8xp
changelog.txt
TREKGUI.8xv
Here is the contents of the ZIP after the script runs (once):
Code:
titrek_0.0.28.zip /
bugs.xml
titrek_0.0.28 /
TITREK.8xp
changelog.txt
TREKGUI.8xv
Can anyone shed some light on why this is occurring and how to prevent it?