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

时间:2016-09-15 17:04:46

标签: 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);
    }
?>

我在页面重新加载或指向它时收到此错误

  

注意:未定义的索引:第21行的C:\ xampp \ htdocs \ profilepic.php中的myimage

     

注意:未定义的索引:第22行的C:\ xampp \ htdocs \ profilepic.php中的myimage

但是一旦文件上传,错误就会消失,请帮助。下面是我的HTML代码

<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 个答案:

答案 0 :(得分:1)

首次加载页面时,$_FILES['myimage']['name']$_FILES['myimage']['tmp_name'];未定义,因为您尚未上传任何内容。

解决方案是:

  • 首先在提交按钮中添加name属性,如下所示:

    <button type="submit" name="submit" class="btn btn-info">upload</button>
    
  • 将表单处理代码包装在if块中,如下所示:

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

答案 1 :(得分:0)

老兄,请检查文件var或ur表单数据:

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 :(得分:0)

过去我有类似的问题,我要做的就是检查表单标签,在表单标签中我有<form action="customer_register.php" method="post" enctype="multipart/form-date">,问题是我错误地键入了“ multipart / form-date ”代替“ multipart / form-data”

第二,我检查了以下内容:

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'];

检查上述内容后,我意识到我误输入了$FILES而不是$_FILES

以上情况暗示,如果出现这样的提示Notice: Undefined index: myimage in C:\xampp\htdocs,最好仔细检查PHP中的form标记和isset()函数,以发现任何可能的错误。