0
<?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>
3
  • What is on line 22 of profilepic.php? Have you tried var_dump($_FILES); to make sure the data is in there how you're expecting it? Commented Sep 15, 2016 at 17:07
  • these are lines of code on line 21 & 22 $name=$_FILES['myimage']['name']; $temp_name=$_FILES['myimage']['tmp_name']; Commented Sep 15, 2016 at 17:16
  • Don't use mysql_ database extensions, they were deprecated as of PHP 5.5.0 and were removed altogether in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions. Commented Sep 15, 2016 at 17:24

3 Answers 3

1

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);
        }
    }
    
Sign up to request clarification or add additional context in comments.

13 Comments

but after using the above code and adding name="submit" in <button> in the code image is not getting uploaded in the "profilepics" folder
@hatim Is it the upload problem or you can't see the uploaded image here, ...<img src="<?php $target_file ?>"...? If later one is the issue then change that line in the following way, <div class="pull-right"><img src="<?php if(isset($_POST['submit'])){ echo $location . $name; } ?>" height="150px" width="200px"/></div><br />
I dont know but I cant see the image in the folder
@hatim Make sure that your php page and profilepics folder are in the same directory. Also, wrap your move_uploaded_file() function inside if-else block, like this: if(move_uploaded_file(...)){ echo "success"; }else{ echo "failure"; }
on removing name="submit" from <button> image is uploading in the folder but showing index not defined error
|
0

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

2 Comments

i add if(isset($_FILES))
@RocketHazmat Seems as though he just wrapped everything in an isset($_FILES) if statement
0

I had similar problem in the past, what I did to solve it was to check my form tag, in my form tag I have this, <form action="customer_register.php" method="post" enctype="multipart/form-date"> , the problem was I mistakenly typed "multipart/form-date" instead of "multipart/form-data"

Secondly I checked below:

if(isset($_POST['register'])){

    $ip = getIp(); 

    $c_name = $_POST['c_name'];
    $c_email = $_POST['c_email'];
    $c_pass = $_POST['c_pass'];
    $c_image = $_FILES['c_image']['name'];
    $c_image_tmp = $FILES['c_image']['tmp_name'];
    $c_country = $_POST['c_country'];
    $c_city = $_POST['c_city'];
    $c_contact = $_POST['c_contact'];
    $c_address = $_POST['c_address'];

After checking the above I realized I have mistakenly typed $FILES instead of $_FILES

The above scenario implies that if you get such a prompt Notice: Undefined index: myimage in C:\xampp\htdocs, it is best to check carefully through the form tag and isset() funtion in PHP to identify any possible mistake.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.