首页 > 解决方案 > 图片上传上的未识别索引(PHP,Mysql)

问题描述

我不断收到这个未定义的错误

Undefined index: image in /Applications/XAMPP/xamppfiles/htdocs/buksuapp/completeprofile.php on line 51


我不明白为什么会出现此错误,我已经检查了文件夹是否存在于目录中,授予应用程序权限,放置enctype="multipart/form-data在表单上,​​没有任何效果。

这是我的代码:

HTML

<form action = "" method = "post" runat="server" enctype='multipart/form-data'>
     <input type = "hidden" name = "size" value = "1000000">
     <input type = "file" name = "image" id = "file" accept="image/*" /><br/>
     <input type = "submit" name = "btn_submit" value = "Finish">
</form>

PHP

//Random Generator
function random_str($type = 'alphanum', $length = 8)
{
  switch($type)
  {
      case 'basic'    : return mt_rand();
          break;
      case 'alpha'    :
      case 'alphanum' :
      case 'num'      :
      case 'nozero'   :
              $seedings             = array();
              $seedings['alpha']    = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
              $seedings['alphanum'] = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
              $seedings['num']      = '0123456789';
              $seedings['nozero']   = '123456789';

              $pool = $seedings[$type];

              $str = '';
              for ($i=0; $i < $length; $i++)
              {
                  $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
              }
              return $str;
          break;
      case 'unique'   :
      case 'md5'      :
                  return md5(uniqid(mt_rand()));
          break;
  }
}

if(isset($_POST['btn_submit'])){
  $random_gen = random_str('alphanum', 10);
  $random_gen2 = random_str('alphanum', 10);


  $file = $_FILES['image'];

  #Get File Properties
  $file_name = $file['name'];
  $file_tmp = $file['tmp_name'];
  $file_size = $file['size'];
  $file_error = $file['error'];

  #Get File Extension
  $file_ext = explode('.', $file_name);
  $file_ext = strtolower((end($file_ext)));



  $file_rand_name = "buksunetIMG_".$random_gen."_".$random_gen2;

  $file_name_new = $file_rand_name.".".$file_ext;
  $file_destination = "./profileimgs/".$file_name_new;

  if(move_uploaded_file($file_tmp, $file_destination)){
    #File has been uploaded successfully
    echo "<br/><br/>Successful!";
  } else {
    echo "<br/><br/>Error!";
  }

}//END OF SUBMIT BUTTON


我在 youtube 上观看了与此类似的主题的教程,我们有相同的代码。然而他们的工作,我仍然得到这个未定义的错误。我怎样才能解决这个问题?

标签: phphtmlmysql

解决方案


添加if(isset($_FILES['image']))

<form action = "" method = "post" runat="server" enctype='multipart/form-data'>
     <input type = "hidden" name = "size" value = "1000000">
     <input type = "file" name = "image" id = "file" accept="image/*" /><br/>
     <input type = "submit" name = "btn_submit" value = "Finish">
</form>

<?php

//Random Generator
function random_str($type = 'alphanum', $length = 8)
{
  switch($type)
  {
      case 'basic'    : return mt_rand();
          break;
      case 'alpha'    :
      case 'alphanum' :
      case 'num'      :
      case 'nozero'   :
              $seedings             = array();
              $seedings['alpha']    = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
              $seedings['alphanum'] = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
              $seedings['num']      = '0123456789';
              $seedings['nozero']   = '123456789';

              $pool = $seedings[$type];

              $str = '';
              for ($i=0; $i < $length; $i++)
              {
                  $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
              }
              return $str;
          break;
      case 'unique'   :
      case 'md5'      :
                  return md5(uniqid(mt_rand()));
          break;
  }
}

if(isset($_POST['btn_submit'])){
  $random_gen = random_str('alphanum', 10);
  $random_gen2 = random_str('alphanum', 10);

if(isset($_FILES['image'])){

  $file = $_FILES['image'];

  #Get File Properties
  $file_name = $file['name'];
  $file_tmp = $file['tmp_name'];
  $file_size = $file['size'];
  $file_error = $file['error'];

  #Get File Extension
  $file_ext = explode('.', $file_name);
  $file_ext = strtolower((end($file_ext)));



  $file_rand_name = "buksunetIMG_".$random_gen."_".$random_gen2;

  $file_name_new = $file_rand_name.".".$file_ext;
  $file_destination = "up/".$file_name_new;

  if(move_uploaded_file($file_tmp, $file_destination)){
    #File has been uploaded successfully
    echo "<br/><br/>Successful!";
  } else {
    echo "<br/><br/>Error!";
  }

}//END OF SUBMIT BUTTON
}
?>

推荐阅读