Lefteris008
Lefteris008

Reputation: 906

Form text field returns empty in PHP, though there is always a value

I have developed a website in which, amongst other features, a user can upload files in a single 'dataset'. For this feature, I have created a specific php page ('update.php') in which the user can define a name for this dataset and upload his files. Then, these files are stored in a directory and I update a simple MySQL DB with the relevant data (name of dataset, flags for every file that he had uploaded etc). I've developed and tested the website in XAMPP on Windows 8. The problem is that now, that I have migrated the entire project in Apache (on Ubuntu 10.04 LTS) I'm facing strange problems with the creation of the datasets and the uploads of files. The most strange problem is that, when I POST the form text field of the dataset in a PHP variable, it is always empty, although I have a default value every time. More importantly, none of the files are uploaded to the directory, although I have given proper rights to the folders and the website (chmod 0777, added the user I'm logged in to the 'www-data' group etc). I have searched the Google for this strange behaviour but none of the pages are helpful. Also, I am convinced that this is not a PHP error but for your help, here's a simple code snippet from the 'update.php':

if($submit == "data") {
   $old_ds_name = $_GET['name'];
   $type = $_GET['type'];
   $ds_name = $_POST['ds-name'];
   $connect = mysql_connect("localhost", "user", "password");
   if(!$connect) {
      die('Connection error: ' . mysql_error());
   }
   $select = mysql_select_db("db", $connect);
   if(!$select) {
      die('Invalid DB: ' . mysql_error());
   }
   //Check names
   if($old_ds_name != $ds_name) {
      $query1 = "SELECT name FROM datasets WHERE name='$ds_name'";
      $result1 = mysql_query($query1);
      $do_name_check = mysql_num_rows($result1);
      if($do_name_check > 0) {
         ?>
         <script type="text/javascript">
            var ds2 = '<?php echo $ds_name; ?>';
            alert('Dataset '+ds2+' already exists. Please choose a different name.');
            <?php 
            mysql_close();
            ?>
            window.history.back();
         </script>
         <?php

      }
   }
   $query2 = "SELECT * FROM datasets WHERE name='$old_ds_name'";
   $result2 = mysql_query($query2);
   $dir_name = mysql_result($result2,0, "dir_name");
   if($type == 0) { //Normal dataset 
      $neo4j_f = mysql_result($result2,0, "neo4j");
      if(isset($_POST['mentions']) && $_POST['mentions'] == "Yes") {
         $mentions = "1";
      }
      mysql_close();
      if($_FILES["neo4j"]["name"]) {
         $filename = $_FILES["neo4j"]["name"];
         $source = $_FILES["neo4j"]["tmp_name"];
         $type = $_FILES["neo4j"]["type"];
         $neo4j_f = uploadNeo4j($filename, $source, $type, $dir_name, $neo4j_f);
         if($neo4j_f == -1) {
            die('<script type="text/javascript"> 
               alert("The Neo4j DB file you are trying to upload is not a .zip file.");
               window.history.back();
               </script>');
         }
         elseif($neo4j_f == -2) {
               die('<script type="text/javascript"> 
                   alert("An error occured while uploading youe Neo4j DB file. Please try again.");
                   window.history.back();
                   </script>');
         }
         else {
            $neo4j_f = "1";
         }
      }
      $ds_name1 = mysql_real_escape_string($ds_name);
      $old_ds_name1 = mysql_real_escape_string($old_ds_name);
      if($old_ds_name1 != $ds_name1) {
         $query = "UPDATE datasets SET name='$ds_name1', neo4j='$neo4j_f', mentions='$mentions' WHERE name='$old_ds_name1'";
      } else {
         $query = "UPDATE datasets SET neo4j='$neo4j_f', mentions='$mentions' WHERE name='$old_ds_name1'";
      }
   }
   $connect = mysql_connect("localhost", "user", "password");
   if(!$connect) {
      die('Connection error: ' . mysql_error());
   }
   $select = mysql_select_db("evo_com", $connect);
   if(!$select) {
      die('Invalid DB: ' . mysql_error());
   }
   $result = mysql_query($query);
   if(!$result) {
      die('Invalid query 1: ' . mysql_error());
   }
   mysql_close();
}

The 'uploadNeo4j()' PHP function:

function uploadNeo4j($filename, $source, $type, $dir_name, $neo4j_f) {
    include 'filepaths.php';
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    $okay = false;
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        } 
    }

    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        return -1;
    }

    $target_path = "uploads/".$filename;
    if(move_uploaded_file($source, $target_path)) {
        $zip = new ZipArchive();
        $x = $zip->open($target_path);
        if ($x === true) {
            $dir = $DEFAULT_ROOT_PATH.$DATA_PARENT_FOLDER.$DBs.$dir_name.$NEO_ROOT.$NEO_FOLDER;
            rrmdir($dir);   
            $zip->extractTo($DEFAULT_ROOT_PATH.$DATA_PARENT_FOLDER.$DBs.$dir_name.$NEO_ROOT.$NEO_FOLDER);
            $zip->close();
            $lucene_neo = $DEFAULT_ROOT_PATH.$DATA_PARENT_FOLDER.$DBs.$dir_name.$NEO_ROOT.$NEO_FOR_LUCENE_FOLDER;
            recursive_copy($dir, $lucene_neo);
            unlink($target_path);
            $neo4j_f = 1;
            return $neo4j_f;
        }
    } else {    
        return -2;
    }
}

And the relevant HTML code:

<form id="editdata" enctype="multipart/form-data" action="update.php&action=edit&submit=data&name=<?php echo $_GET['name']; ?>&type=<?php echo $_GET['type']; ?>" method="post">
   <table style="margin:inherit" cellspacing="15px">
      <tr>
         <td><div id="text-label">Name of dataset:</div></td>
         <td><input type="text" id="ds-name" name="ds-name" value="<?php echo $_GET['name']; ?>" required="required"/></td>
      </tr>
      <tr>
         <td><div id="text-label">Date of creation:</div></td>
         <td class="desc"><?php $date = new DateTime($_GET['date']); echo $date->format('d.m.Y H:i:s');?></td>
      </tr>
      <?php
         if($_GET['type'] != 1) {
      ?>
      <tr>
         <td><div id="text-label">Type of dataset:</div></td>
         <td class="desc">Type 1</td>
      </tr>
      <tr>
         <td><div id="text-label">Neo4j Graph DB:</div></td>
         <?php
         if($_GET['neo4j'] == "1") {
         ?>
            <td><div id="text-label-exists">UPLOADED</div></td>
         <?php
         }else {
         ?>
            <td><div id="text-label-not-exists">DOESN'T EXIST</div></td>
         <?php
         }
         ?>
         <td><input type="file" id="neo4j" name="neo4j"/></td>
      </tr>
      <tr>
         <td class="text-label">Relationships:</td>
         <td>
            <div class="desc"><input name="mentions" type="checkbox" <?php if(isset($_GET['mentions']) && $_GET['mentions'] == "1") { echo "checked"; }?> value="Yes"/>Mentions
         </td>
      </tr>
   </table>
   <br/>
   <input style="margin-left: 15px;" id="saveChanges" class="button_text" type="submit" name="submit" value="Save changes"/>
</form>

I have configured the Apache to accept bigger files and to allow bigger execution times , I have granted full rights to my MySQL user but the problem wasn't fixed. The text field I am referring to is the $_POST['ds-name] where as you can see has a default value, but in PHP is empty. Also, I have included an echo function when the PHP execution enters the if($_FILES['neo4j']['name']) but was never displayed. Thanks in advance.

Upvotes: 0

Views: 532

Answers (2)

Lefteris008
Lefteris008

Reputation: 906

Turns out it was the post_max_size property in 'php.ini' which was set to just 8 MB. Changing it to a greater value, solved the problem.

Upvotes: 0

astax
astax

Reputation: 1767

I've seen completely empty POST data in PHP when the uploaded file exceeds the size limit defined in php.ini, so probably you have the same issue.

Check if files upload is enabled, post_max_size and file size limit in php.ini are higher than the file you're uploading (verify with phpinfo()). Note, I'm not saying about Apache configuration, but php.ini

The code that detects too big file:

if (count($_FILES) == 0 && count($_POST) == 0 && count($_GET) == 0 && $_SERVER['CONTENT_LENGTH'] > 1000) // this means uploaded file was too large
    die("Too big file")

Upvotes: 2

Related Questions