2

I am trying to upload image as shown here w3schools But it always shows the error

Undefined index: file 

This is the code

HTML

<form action="upload.php" method="post"  enctype="multipart/form-data">
    <!-- Upload image -->
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload Image" name="submit">
</form>

PHP

<?php
    if(!isset($_POST["submit"])){
        die('Error');
    }
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["file"]["tmp_name"]);
        if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
        } else {
        echo "File is not an image.";
        $uploadOk = 0;
        }
    }
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    if ($_FILES["file"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=   "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file"]["name"]). " has been     uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
?>
Eko Junaidi Salam
  • 1,663
  • 1
  • 18
  • 26
Akshay
  • 14,138
  • 5
  • 46
  • 70

4 Answers4

6

If you are using WAMP or XAMPP they have file upload limit set.If the file size is greater than 2mb the upload will not work.Try uploading images of size less than 2mb and see if it works. To change file upload limit open your php.ini file and modify this value

upload_max_filesize=2M

Replace 2M with limit you wish to provide eg 6M or 8M

The following link explains about changing the file upload limit PHP change the maximum upload file size

Community
  • 1
  • 1
AAB
  • 1,594
  • 2
  • 25
  • 40
1

You can use upload.php like this, It will work.

<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
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>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("c:\wamp\www\upload/newupload/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
pcs
  • 1,864
  • 4
  • 25
  • 49
1
Use this code according to your script


if($_FILES["image1"]["name"]!='')

         {

            $image_name1 = $_FILES["image1"]["name"];
            $source_image1=$_FILES["image1"]["tmp_name"];
        }

            $filename1= time()."_".$image_name1;
            $folderpath1 = ROOT."images/uploads/".$filename1;
                move_uploaded_file($source_image1,$folderpath1);
            if(!move_uploaded_file($source_image1,$folderpath1))
            {
                $err.="There is problem to upload the Image please try again";
            } 
Rahul Gahlot
  • 68
  • 2
  • 14
1
if(isset($_POST['submit'])){


    function PhotoUpload($imagename,$conn)
    {                    

        if(isset($imagename))
        {
            $filename = $_FILES[$imagename]['name'];
            $filesize = $_FILES[$imagename]['size'];
            $filetype = $_FILES[$imagename]['type'];
            $path = "photo/".$emp_id.".jpg";
            $allowedExtensions = array("tif","png","jpeg","jpg","gif"); 
            move_uploaded_file($_FILES[$imagename]["tmp_name"],"$path");

            $upd1="UPDATE `table` SET `photo`='$path'";
             $sql_upd = mysql_query($upd1);
             $msg='Record Updated Successfully';

        }
    }
    if($_FILES["file"]["error"] == 0)
    {
        PhotoUpload('file',$conn); 
    }

}
Amrita Gupta
  • 37
  • 1
  • 11