上传图像文件时php中的未定义索引

<?php
ob_start();
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysl_database = "login";

$conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
$db_select=mysql_select_db($mysl_database, $conn);
if($conn==false ||$db_select==false)
{
    die("connection error");
}

    if(!isset($_SESSION["email"]))
{
    session_start();
}

$location="profilepics/";
$name=$_FILES['myimage']['name'];
$temp_name=$_FILES['myimage']['tmp_name'];
if((isset($name)))
{
        move_uploaded_file($temp_name,$location.$name);
    }
?>

I am getting this error while the page gets reload or directed towards it

Notice: Undefined index: myimage in C:\xampp\htdocs\profilepic.php on line 21

Notice: Undefined index: myimage in C:\xampp\htdocs\profilepic.php on line 22

but as soon as file is uploaded errors disappear please help.below is my html code

<form method="post" enctype="multipart/form-data" action="profilepic.php">
  <div id="box_title"><span class="glyphicon glyphicon-user"></span> <strong>I look like this: </strong></div>
  <div class="pull-right"><img src="<?php $target_file ?>" height="150px" width="200px"/></div><br />
  <div class="form-group">
    <label for="exampleInputFile">select a picture</label>
    <input type="file" id="exampleInputFile" name="myimage" />
    <p class="help-block">upload your photo</p>
  </div>
  <button type="submit" class="btn btn-info">upload</button>
  <br />
  </form>

When you first load the page, $_FILES['myimage']['name'] and $_FILES['myimage']['tmp_name']; will be undefined because you haven't uploaded anything yet.

The solution is:

  • First add a name attribute your submit button, like this:

    <button type="submit" name="submit" class="btn btn-info">upload</button>
    
  • And wrap your form processing code inside an if block, like this:

    // your code
    
    if(isset($_POST['submit'])){
        $location="profilepics/";
        $name=$_FILES['myimage']['name'];
        $temp_name=$_FILES['myimage']['tmp_name'];
        if(isset($name)){
            move_uploaded_file($temp_name,$location.$name);
        }
    }
    

Dude please check file var or ur form data:

if(isset($_FILES)){
ob_start();
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysl_database = "login";

$conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
$db_select=mysql_select_db($mysl_database, $conn);
if($conn==false ||$db_select==false)
{
    die("connection error");
}

    if(!isset($_SESSION["email"]))
{
    session_start();
}

$location="profilepics/";
$name=$_FILES['myimage']['name'];
$temp_name=$_FILES['myimage']['tmp_name'];
if((isset($name)))
{
        move_

uploaded_file($temp_name,$location.$name);
}
}