图像上传中的未识别索引(PHP,Mysql)

时间:2018-05-02 21:50:34

标签: php html 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上观看了类似主题的教程,我们有相同的代码。然而他们的工作,我仍然得到这个未定义的错误。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

添加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
}
?>