php文件上传系统 - 未定义的索引

时间:2014-03-21 23:47:49

标签: php upload xampp

我收到以下错误,我不知道为什么。

Notice: Undefined index: myfile in C:\xampp\htdocs\upload.php on line 3
Notice: Undefined index: myfile in C:\xampp\htdocs\upload.php on line 4
Notice: Undefined index: myfile in C:\xampp\htdocs\upload.php on line 5
Notice: Undefined index: myfile in C:\xampp\htdocs\upload.php on line 6
Notice: Undefined index: myfile in C:\xampp\htdocs\upload.php on line 7

请帮忙,我是初学者...我的代码:

 <html>//this is index.html

 <form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="myfile">File limit 1MB<p>
    <input type="submit" value="Upload">
 </form>
 </html>

这是我的upload.php脚本

<?php //this is my upload.php script    
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];

$temp = $_FILES["myfile"]["tmp_name"]; 
$error = $_FILES["myfile"]["error"];//size  
if ($size > 1000000){
die("The file size is too big!");}

else
move_uploaded_file($temp, "uploaded/" .$name);//move upload file    
?>

1 个答案:

答案 0 :(得分:0)

也许以下内容会有所帮助?

以下代码会将gif,jpeg,jpg,png文件上传到名为uploads的文件夹中。

表单上传

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

<强> upload.php的

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


    if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);

      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 

您可能必须将文件夹CHMOD为777以允许写入文件。