-3

i am trying to upload an image.

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

After Posting Data,

<?php

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

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

but it does not upload the file.

abby
  • 680
  • 2
  • 10
  • 22

5 Answers5

1

make sure that you have specified enctype in form tag

<form action="test.php" enctype="multipart/form-data" method="POST">
Nauphal
  • 6,194
  • 4
  • 27
  • 43
1

Change this:-

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

to

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

You have used id="files" in form but you are using "file" in your php code.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
1

i think it should be like

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

in your $path

ABorty
  • 2,512
  • 1
  • 13
  • 16
0

YOU have error in

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);
  }
martian
  • 20
  • 5
0

According to wat I understood ur problem is with only file upload..So I have briefed the php and html file for you..Comments are there in the code and I have tried to keep things easy. You can get the idea about the code through the comments. Any more queries wud be appreciated.

 //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>
Divyang
  • 372
  • 1
  • 5
  • 21