-3

我正在尝试上传图片。

 <input name="files" type="file" id="files">
<input type="submit" name="submit" id="submit" value="Add" />

发布数据后,

<?php

if(isset($_POST['submit']))
{
$path="panel/images"."/".$_FILES["file"]["name"];

move_uploaded_file($_FILES["files"]["tmp_name"], $path);
}
?>

但它不会上传文件。

4

5 回答 5

1

确保您已enctypeform标签中指定

<form action="test.php" enctype="multipart/form-data" method="POST">
于 2013-06-28T08:56:36.400 回答
1

改变这个: -

$path="panel/images"."/".$_FILES["file"]["name"];

$path="panel/images"."/". basename($_FILES["files"]["name"]);

您已id="files"在表单中使用,但您在 php 代码中使用“文件”。

于 2013-06-28T08:57:47.463 回答
1

我认为应该是这样的

$_FILES["files"]["name"] not $_FILES["file"]["name"]

在你的 $path

于 2013-06-28T08:58:24.290 回答
0

你有错误

if(isset($_POST['submit']))
 {
   $path="panel/images"."/".$_FILES["files"]["name"];
  //This Line Your element name is files not file

    move_uploaded_file($_FILES["files"]["tmp_name"], $path);
  }
于 2013-06-28T09:02:35.553 回答
0

据我了解你的问题是只有文件上传..所以我已经为你简要介绍了 php 和 html 文件..代码中有注释,我试图让事情变得简单。您可以通过注释了解有关代码的想法。任何更多的疑问都将不胜感激。

 //This is the php code for upload file..
 <?php
 //allowedExts variable is an array consisting of file types that can be supported.we are uploading image so image file extensions are used.
$allowedExts = array("gif", "jpeg", "jpg", "png");
//explode function breaks the string, here used to get the file extension, whenever 
//a dot(.) would be found in the filename,say abc.jpg, the extension jpg would be 
//retrived.
$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"] < 20000)
&& in_array($extension, $allowedExts))
 {
 //if any error in file, display it
 if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
 //else upload the file
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
 //file will be uploaded in the upload directory..but also need to check whether 
 //the directory consists a same filename already or not..if it does contains
 // a file say abc.jpg already..it would display the msg file already exist
if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
//else it will upload it in upload directory, you can name the directory acc to you..
//but conventionally upload is used as the directory name
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}
 }
else
 {
  echo "Invalid file";
 }
 ?>
//php file ends here
//this is the html file..frame it according to wat u need
<html>
<body>
//the most important thing is that you should keep the 
//enctype(encryption type)=multipart/form-data. This is mandatory for media files
<form action="upload_file.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>

</body>
</html>
于 2013-06-28T09:33:40.920 回答