Trying to upload the a file

时间:2017-06-15 10:21:13

标签: php html

I am trying to Build a Website with file uploading! Here is my code: index.php:

<?php include('/inc/header.php'); ?>
<?php
if($_SESSION['username'] != null)
{
    echo "
    <form class=\"form-horizontal\" method=\"post\" action=\"inc/sumbitfinish.php\" enctype=\"multipart/form-data\">

<!-- Text input-->
<div class=\"form-group\">
  <label class=\"col-md-4 control-label\" for=\"Your Name\">Name</label>  
  <div class=\"col-md-4\">
  <input id=\"Your Name\" name=\"namew\" type=\"text\" placeholder=\"placeholder\" class=\"form-control input-md\" required=\"\">

  </div>
</div>

<!-- Text input-->
<div class=\"form-group\">
  <label class=\"col-md-4 control-label\" for=\"Home Work Code\">Home Work Code</label>  
  <div class=\"col-md-4\">
  <input id=\"Home Work Code\" name=\"code\" type=\"text\" placeholder=\"placeholder\" class=\"form-control input-md\">

  </div>
</div>

<!-- Textarea -->
<div class=\"form-group\">
  <label class=\"col-md-4 control-label\" for=\"textarea\">Additional Imformation </label>
  <div class=\"col-md-4\">                     
    <textarea class=\"form-control\" id=\"textarea\" name=\"add\">default text</textarea>
  </div>
</div>

<!-- File Button --> 
<div class=\"form-group\">
  <label class=\"col-md-4 control-label\" for=\"filebutton\">File Button</label>
  <div class=\"col-md-4\">
    <input id=\"ufile\" name=\"ufile\" class=\"input-file\" type=\"file\">
  </div>
</div>

<!-- Button -->
<div class=\"form-group\">
  <label class=\"col-md-4 control-label\" for=\"\"></label>
  <div class=\"col-md-4\">
    <button id=\"Submit\" name=\"sumbit\" value=\"Sumbit\" class=\"btn btn-primary\">Hand In</button>
  </div>
</div>


</fieldset>
</form>
";
}
else
{
        echo "<script language=\"javascript\">";
        echo "alert(\"You don\'t have permission to access this page\")";
        echo "</script>";
        echo "<meta http-equiv=REFRESH CONTENT=0;url=/index.php>";
};
?>

sumbitfinish.php

<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include('mysql.php');

echo $_FILES["file"]["name"];
?>

I have simplify the upload code , But it still show the error massage

Undefined index: file in C:\xampp\htdocs\system\inc\sumbitfinish.php on line 6 

What is the problem? I need also send the data to MySQL at the same time , It that this is function cannot work in the same time?

1 个答案:

答案 0 :(得分:0)

The ID of the input field of type file is ufile, not file, so try $_FILES["ufile"]["name"].