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:
<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?
I admit that I'm not some PHP guru but I don't see anything right off the bat. I do think you need more debug statements. It's great that you echo echo $fileToOpen; but I don't see you echo $fileToModify;.

Secondly, you should add echo statements to each of your IF conditions so you know what is being ran. Sure, you have echo "Creating new string"; when $oldData is false, so the else part of that statement should be executed but how do you know it is? Let's add a debug statement there so we can be sure. If neither debug statement is echo'd then you know that the IF statement above it isn't being met (it isn't TRUE).

I truly think if you add more debug statements you'll find your problem.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement