Well, I am making an upload for people who make games with the Unity game engine. Basically, the engine will build the game and produce an html file and .unity3d file.

here is my html form:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Index file:</label>
<input type="file" name="file" id="file" />
<br />
<label for="file2">Unity file:</label>
<input type="file" name="file2" id="file2" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

and here is my upload_file script:

<?php
if ((($_FILES["file"]["type"] == "text/html")
&& ($_FILES["file2"]["size"] < 8388608)
&& ($_FILES["file"]["size"] < 8388608)))
{
if($_FILES["file2"]["size"] < 8388608){
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    echo "Return Code: " . $_FILES["file2"]["error"] . "<br />";
    }
  else
    {
    $mb = $_FILES["file2"]["size"] / 1048576;
    echo "Done.";
    echo "<br />";
    echo "Index file: " . $_FILES["file"]["name"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "<br />";
    echo "Unity file: " . $_FILES["file2"]["name"] . "<br />";
    echo "Size: " . ($_FILES["file2"]["size"] / 1048576) . "Mb<br />";
    echo "<br />";
    if($mb < 1){
    echo "Space used: [-------------]";
    }
    else if($mb <= 1){
    echo "Space used: [|------------]";
    }
    else if($mb <= 2){
    echo "Space used: [||-----------]";
    }
    else if($mb <= 3){ 
    echo "Space used: [|||----------]";
    }
    else if($mb <= 4){
    echo "Space used: [|||||--------]";
    }
    else if($mb <= 5){
    echo "Space used: [|||||||------]";
    }
    else if($mb <= 6){
    echo "Space used: [|||||||||----]";
    }
    else if($mb <= 7){
    echo "Space used: [|||||||||||--]";
    }
    else if($mb <= 8){
    echo "Space used: [|||||||||||||]";
    }
    echo "<br />";
    echo "<br />";
    $random = rand(100, 999);
    mkdir($random);
    move_uploaded_file($_FILES["file2"]["tmp_name"],
      "upload" . "/" . $_FILES["file"]["name"]);
      move_uploaded_file($_FILES["file"]["tmp_name"],
      $random . "/index.html");
      
      echo "Location: <a href=" . $random . "/>" . $random . "/</a>";
      
    }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

But in order for the file to work, it needs the .unity3d program. And for some reason, it's not copying over. Is there something wrong with my code?

Dani AI

Generated

The upload completes in the browser but the .unity3d never appears on disk most often because of one of two things: the script moves the file to the wrong location (so the HTML can’t find it), or the server refuses the move (permissions, PHP limits, or server restrictions). As suggested, confirm the folder you create is writable. Also inspect what PHP actually received: dump the full $_FILES array and the ['error'] values, and check the boolean return from move_uploaded_file so you can see whether the file move itself failed.

A simple safe pattern to follow (adapt the field names to your form) is:

$target = __DIR__ . '/uploads/' . rand(100,999);
if (!is_dir($target) && !mkdir($target, 0755, true)) {
  error_log("mkdir failed: $target");
}
if (is_uploaded_file($_FILES['unity_upload']['tmp_name'])) {
  $dest = $target . '/' . basename($_FILES['unity_upload']['name']);
  if (!move_uploaded_file($_FILES['unity_upload']['tmp_name'], $dest)) {
    error_log('move failed: ' . print_r($_FILES['unity_upload'], true));
  }
}

Extra checks and tips:

  • Make sure both files end up in the same folder if the Unity index.html uses a relative path to the .unity3d file. If the index expects the asset next to it, putting the .unity3d somewhere else will break the embed.
  • Temporarily set directory ownership to the web user and relax perms for testing: chown -R www-data:www-data /path/to/uploads and chmod 755 /path/to/uploads. Revert to tighter perms once working.
  • Check PHP limits (upload_max_filesize, post_max_size) and inspect server/PHP error logs and open_basedir restrictions if writes silently fail.
  • Don’t trust $_FILES['...']['type'] for validation; use finfo_file() to check MIME, and always sanitize filenames with basename().

See the PHP docs for reliable references on file uploads and move_uploaded_file (move_uploaded_file, file upload errors).

It might be a permissions issue with the folder/directory you are randomly creating. You may need to chmod it after you create it.

That didn't work. Google chrome says something is uploading and the stats display the size and everything.


for the index file, upload an html document. The next field is the main concern.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.