正在上传图片

时间:2013-06-28 08:55:20

标签: php upload

我正在尝试上传图片。

 <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);
}
?>

但它不会上传文件。

5 个答案:

答案 0 :(得分:1)

确保您在enctype代码

中指定了form
<form action="test.php" enctype="multipart/form-data" method="POST">

答案 1 :(得分:1)

更改: -

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

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

您在表单中使用了id="files",但您在php代码中使用了“file”。

答案 2 :(得分:1)

我认为它应该像

$_FILES["files"]["name"] not $_FILES["file"]["name"]
你的$ path中的

答案 3 :(得分: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);
  }

答案 4 :(得分:0)

根据wat我明白你的问题是只有文件上传..所以我已经为你简要介绍了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>
相关问题