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>
Community
  • 1
  • 1
hatim
  • 23
  • 2
  • 6
  • 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? – Blake Sep 15 '16 at 17:07
  • these are lines of code on line 21 & 22 $name=$_FILES['myimage']['name']; $temp_name=$_FILES['myimage']['tmp_name']; – hatim Sep 15 '16 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](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul Sep 15 '16 at 17:24

3 Answers3

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);
        }
    }
    
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • but after using the above code and adding name="submit" in – hatim Sep 15 '16 at 17:35
  • @hatim Is it the upload problem or you can't see the uploaded image here, `...
    `
    – Rajdeep Paul Sep 15 '16 at 17:45
  • I dont know but I cant see the image in the folder – hatim Sep 15 '16 at 17:50
  • @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"; }` – Rajdeep Paul Sep 15 '16 at 17:56
  • on removing name="submit" from – hatim Sep 15 '16 at 18:08
  • @hatim Not sure what exactly you're trying to do. Paste your complete code on [`pastebin.com`](http://pastebin.com/) and give me it's link here. – Rajdeep Paul Sep 15 '16 at 18:11
  • can I get ur email actually the pastebin.com is not responding – hatim Sep 15 '16 at 18:23
  • @hatim Before diving deep into the code, preliminary things say, 1) you didn't include `name="submit"` attribute as I suggested in my answer. 2) Are you getting any `success` or `error` message after submitting the form? – Rajdeep Paul Sep 15 '16 at 18:36
  • I made the changes in the code before uploading the code....and the code is showing nothing neither success or fail – hatim Sep 15 '16 at 18:39
  • @hatim I just added `name="submit"` attribute in the submit `button`, changed the upload directory(as per my system) here, `$location="profilepics/";` and it's working perfectly fine. Just check whether your `$location . $name` path is correct or not. – Rajdeep Paul Sep 15 '16 at 18:43
  • Are the images moving to the "profilepics/" folder ??? can you please mail me the final code in which you have made changes – hatim Sep 15 '16 at 18:46
  • thank you It finally worked..with no errors...thanks brother – hatim Sep 15 '16 at 19:08
  • @hatim You're welcome! Please mark the answer as *accepted* if resolved your issue. [How to accept answer on Stack Overflow?](http://meta.stackexchange.com/a/5235) – Rajdeep Paul Sep 15 '16 at 19:09
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);
}
}
MSS
  • 3,520
  • 24
  • 29
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.

aso
  • 1,331
  • 4
  • 14
  • 29
Agbley
  • 1
  • 2