dream3323
2016-09-15 17:04
浏览 90
已采纳

上传图像文件时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>

图片转代码服务由CSDN问答提供 功能建议

 &lt;?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(“连接错误”); 
} 
 
 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); 
} 
?  &gt; 
   
 
 

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

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

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

但是一旦文件上传,错误就会消失 请help.below是我的html代码

 &lt; form method =“post”enctype =“multipart / form-data”action =“profilepic.php”&gt; 
  &lt; div id =“box_title”&gt;&lt; span class =“glyphicon glyphicon-user”&gt;&lt; / span&gt;  &lt; strong&gt;我看起来像这样:&lt; / strong&gt;&lt; / div&gt; 
&lt; div class =“pull-right”&gt;&lt; img src =“&lt;?php $ target_file?&gt;”  height =“150px”width =“200px”/&gt;&lt; / div&gt;&lt; br /&gt; 
&lt; div class =“form-group”&gt; 
&lt; label for =“exampleInputFile”&gt; 选择图片&lt; / label&gt; 
&lt; input type =“file”id =“exampleInputFile”name =“myimage”/&gt; 
&lt; p class =“help-block”&gt;上传您的照片&lt; / p&gt  ; 
&lt; / div&gt; 
&lt; button type =“submit”class =“btn btn-info”&gt; upload&lt; / button&gt; 
&lt; br /&gt; 
&lt; / form&gt; 
    
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucao1066 2016-09-15 17:15
    已采纳

    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);
          }
      }
      
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongshandun4363 2016-09-15 17:08

    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);
    }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题