duanping2809 2013-08-13 12:07
浏览 42
已采纳

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>
  • 写回答

1条回答 默认 最新

  • duangan6133 2013-08-13 12:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。