PHP上传和设置文件属性的问题

I am currently trying to add an upload system where a user can upload their own file. They will be able to:

  • Name it
  • Categorize It
  • Add a Description
  • Add Instructions for it's use
  • Add a thumbnail for it

I have begun by just adding the ability to name it, however I have stumbled at the first stage!

I have very limited knowledge of PHP (I'm a static HTML guy), so I tried to accomplish my goal through the help of others and online tutorials. Find my code sample below.

Here is a link to the live development site » Hopefully the site will help you to see what I want to do and what goes wrong!

if ($_FILES["mod-name"]["error"] > 0)
  {
  echo "Error: " . $_FILES["mod-name"]["error"] . "<br>";
  }
else
  {
  echo "Upload: " . $_FILES["mod-name"]["name"] . "<br>";
  echo "Type: " . $_FILES["mod-name"]["type"] . "<br>";
  echo "Size: " . ($_FILES["mod-name"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["mod-name"]["tmp_name"];
  }
?>

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["mod-name"]["name"]);
$extension = end($temp);
if ((($_FILES["mod-name"]["type"] == "image/gif")
|| ($_FILES["mod-name"]["type"] == "image/jpeg")
|| ($_FILES["mod-name"]["type"] == "image/jpg")
|| ($_FILES["mod-name"]["type"] == "image/pjpeg")
|| ($_FILES["mod-name"]["type"] == "image/x-png")
|| ($_FILES["mod-name"]["type"] == "image/png"))
&& ($_FILES["mod-name"]["size"] < 500000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["mod-name"]["error"] > 0)
    {
    echo "Error: " . $_FILES["mod-name"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["mod-name"]["name"] . "<br>";
    echo "Type: " . $_FILES["mod-name"]["type"] . "<br>";
    echo "Size: " . ($_FILES["mod-name"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["mod-name"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["mod-name"]["name"]);
$extension = end($temp);
if ((($_FILES["mod-name"]["type"] == "text/plain"))
&& ($_FILES["mod-name"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["mod-name"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["mod-name"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["mod-name"]["name"] . "<br>";
    echo "Type: " . $_FILES["mod-name"]["type"] . "<br>";
    echo "Size: " . ($_FILES["mod-name"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["mod-name"]["tmp_name"] . "<br>";

    if (file_exists("/mod/" . $_FILES["mod-name"]["name"]))
      {
      echo $_FILES["mod-name"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["mod-name"]["tmp_name"],
      "upload/" . $_FILES["mod-name"]["name"]);
      echo "Stored in: " . "/mod/" . $_FILES["mod-name"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

My HTML:

<form method="post" action="upload_file.php" enctype='multipart/form-data'>
  <fieldset>
    <legend>Please fill in every field below!</legend>
    <div class="form-group">
      <label for="ModName">Mod Name</label>
      <input type="text" class="form-control" id="mod-name" placeholder="Enter the mod's name">
    </div>
    <div class="form-group">
      <label for="ModDesc">Description</label>
            <textarea type="text" class="form-control" id="mod-desc" placeholder="Write a little bit about your mod..." rows="4"></textarea>
    </div>
    <div class="form-group">
      <label for="ModInstall">How to install your Mod:</label>
      <textarea type="text" class="form-control" id="mod-install" placeholder="Help us know how to use your mod!" rows="3"></textarea>
    </div>
    <div class="form-group">
      <label for="ModThumb">Add thumbnail</label>
      <input type="file" id="mod-thumb">
      <p class="help-block">Max file size of image: 300kb</p>
    </div>
    Select the Category(s) that your mod falls under:<br /> 
    <div class="checkbox-inline">
      <label>
        <input type="checkbox"> Client Mod
      </label>
    </div>
    <div class="checkbox-inline">
      <label>
        <input type="checkbox"> Game Tweak
      </label>
    </div>
    <div class="checkbox-inline">
      <label>
        <input type="checkbox"> .CUB Model
      </label>
    </div><br />
    <div class="checkbox-inline">
      <label>
        <input type="checkbox"> Server Mod
      </label>
    </div>
    <div class="checkbox-inline">
      <label>
        <input type="checkbox"> Web Tool
      </label>
    </div>
    <div class="checkbox-inline">
      <label>
        <input type="checkbox"> Map Editior
      </label>
    </div>
    <hr />

    <div class="form-group">
      <label for="ModThumb">Upload your Mod!</label>
      <input type="file" id="mod">
      <p class="help-block">Max file size: 5MB (sorry)</p>
    </div>
    <button type="submit" class="btn-lg btn-success">Add it to the Database!</button>
  </fieldset>
</form>

Ok... I think everything is a bit confusing here. First of, add change your form tag to:

<form method="post" action="upload_file.php" enctype='multipart/form-data'>

That should allow you to upload files. Then, every single file you upload must have a different name that php would later recognise, so change your file inputs to this:

<input type="file" id="mod-thumb" name="thumbnail">
<input type="file" id="mod" name="mod_file">

Notice the "name".

Next, go to your upload_file.php... See, every time you go "$_FILES["mod-name"]" you should actually go $_FILES["thumbnail"] for the thumbnail file and $_FILES["mod_file"] for the mod file itself. Change the code accordingly.

I just went through your live site and just adding the name tag for a file to "mod-name" gives you output, so file uploading is allowed. Understand that php needs a hook to refer to each file you want to upload and "name" is your hook there.