title: Undefined Array Key error when uploading image on php
body: <p>im new here, hope to grow into this community.
  I am currently learning php and i am making a program to upload a file. i found a tutorial with some code that makes a little sense to me but not really as i dont understand the lingo in php too well yet... so i have made the api to upload an image and i am testing via postman... so when i test im running a command in json to say <code>{&quot;upfile&quot;:&quot;*whatever the base 64 encode is*&quot;}</code>.when i run this i get an error saying</p>
  <pre><code>Warning: Undefined array key &quot;upfile&quot; in C:\xampp\htdocs\upload.php on line 37
  
  Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\upload.php on line 37
  </code></pre>
  <p>i have tried to remove the ! from an if statement, which i will show below, but that give an invalid parameters error instead... any idea what i am doing wrong?</p>
  <pre><code>&lt;?php
  
  header('Content-Type: text/plain; charset=utf-8');
  
  $server_url = '/xampp/htdocs';
  $uploadDirectory = &quot;/pic/&quot;;
  
  $target_dir = $server_url.$uploadDirectory; // add the specific path to save the file
  
  //$headers = getRequestHeaders();
  //
  //$arr = array();
  //
  //foreach ($headers as $header =&gt; $value) {
  ////    echo &quot;$header: $value &lt;br /&gt;\n&quot;;
  //    array_push($arr,&quot;$header: $value&quot;);
  //}
  //
  //
  ////    $arr = array('result'=&gt;'mime type '.$mime_type,'ext '=&gt;$ext);
  //    throw new RuntimeException(json_encode(print_r($arr)));
  
  //    echo json_encode('mime_type = '.$mime_type.' extension = '.$ext);
  
  try {
  //echo 'Files Array ';
  //echo &quot;\r\n&quot;;
  //echo print_r($_FILES);
  
  //    throw new RuntimeException(json_encode($_FILES['upfile']['tmp_name']));
  //die('here');
  
      // Undefined | Multiple Files | $_FILES Corruption Attack
      // If this request falls under any of them, treat it invalid.
      if (
       isset($_FILES['upfile']['error']) ||     //if i use !isset,  i get invalid parameters
          is_array($_FILES['upfile']['error'])
      ) {
          throw new RuntimeException('Invalid parameters.');
      }
  
      // Check $_FILES['upfile']['error'] value.
      switch ($_FILES['upfile']['error']) {
          case UPLOAD_ERR_OK:
              break;
          case UPLOAD_ERR_NO_FILE:
              throw new RuntimeException('No file sent.');
          case UPLOAD_ERR_INI_SIZE:
          case UPLOAD_ERR_FORM_SIZE:
              throw new RuntimeException('Exceeded filesize limit.');
          default:
              throw new RuntimeException('Unknown errors.');
      }
  
      // You should also check filesize here.
      if ($_FILES['upfile']['size'] &gt; 1000000000) {
          throw new RuntimeException('Exceeded filesize limit.');
      }
  
      // DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
      $finfo = finfo_open(FILEINFO_MIME_TYPE);
      $mime_type = finfo_file($finfo, $_FILES['upfile']['tmp_name']);
  //    $mime_type = finfo_buffer(finfo_open(), $_FILES['upfile']['name'], FILEINFO_MIME_TYPE); // extract mime type
      $ext = mime2ext($mime_type); // extract extension from mime type
  
  
      $ext1 = mime2ext($_FILES['upfile']['type']);
  
      if ($ext !== $ext1) {
          $ext=$ext1;
      }
  
  
  //    $arr = array('result'=&gt;'mime type '.$mime_type,'ext '=&gt;$ext);
  //    throw new RuntimeException(json_encode($arr));
  
  //    echo json_encode('mime_type = '.$mime_type.' extension = '.$ext.' FILES type = '.$_FILES['upfile']['type'].' exte 1 = '.$ext1);
  
  //
  //
  //    // Check MIME Type by yourself.
  //    $finfo = new finfo(FILEINFO_MIME_TYPE);
  //    if (false === $ext = array_search(
  //            $finfo-&gt;file($_FILES['upfile']['tmp_name']),
  //            array(
  //                'jpg' =&gt; 'image/jpeg',
  //                'png' =&gt; 'image/png',
  //                'gif' =&gt; 'image/gif',
  //            ),
  //            true
  //        )) {
  //        throw new RuntimeException('Invalid file format.');
  //    }
  
      // You should name it uniquely.
      // DO NOT USE $_FILES['upfile']['name'] WITHOUT ANY VALIDATION !!
      // On this example, obtain safe unique name from its binary data.
  
      $newfilename = uniqid();//sha1_file($_FILES['upfile']['tmp_name']);
  
      $file = $newfilename.&quot;.&quot;.$ext;
  
      if (!move_uploaded_file(
          $_FILES['upfile']['tmp_name'],
          sprintf($target_dir.'%s.%s',
              $newfilename,
              $ext
          )
      )) {
          $arr = array('result'=&gt;'Failed to move uploaded file.','filename'=&gt;$file);
          throw new RuntimeException(json_encode($arr));
      }
  
  
  
      $arr = array('result'=&gt;'File Uploaded Sucessfully','filename'=&gt;$file);
  //        echo json_encode(&quot;File Uploaded Successfully&quot;);
      echo json_encode($arr);
  //    echo json_encode();
  
  } catch (RuntimeException $e) {
  
      echo $e-&gt;getMessage();
  
  }
  
  function mime2ext($mime){
      $all_mimes = '{&quot;png&quot;:[&quot;image\/png&quot;,&quot;image\/x-png&quot;],&quot;bmp&quot;:[&quot;image\/bmp&quot;,&quot;image\/x-bmp&quot;,
  &quot;image\/x-bitmap&quot;,&quot;image\/x-xbitmap&quot;,&quot;image\/x-win-bitmap&quot;,&quot;image\/x-windows-bmp&quot;,
  &quot;image\/ms-bmp&quot;,&quot;image\/x-ms-bmp&quot;,&quot;application\/bmp&quot;,&quot;application\/x-bmp&quot;,
  &quot;application\/x-win-bitmap&quot;],&quot;gif&quot;:[&quot;image\/gif&quot;],&quot;jpeg&quot;:[&quot;image\/jpeg&quot;,
  &quot;image\/pjpeg&quot;],&quot;xspf&quot;:[&quot;application\/xspf+xml&quot;],&quot;vlc&quot;:[&quot;application\/videolan&quot;],
  &quot;wmv&quot;:[&quot;video\/x-ms-wmv&quot;,&quot;video\/x-ms-asf&quot;],&quot;au&quot;:[&quot;audio\/x-au&quot;],
  &quot;ac3&quot;:[&quot;audio\/ac3&quot;],&quot;flac&quot;:[&quot;audio\/x-flac&quot;],&quot;ogg&quot;:[&quot;audio\/ogg&quot;,
  &quot;video\/ogg&quot;,&quot;application\/ogg&quot;],&quot;kmz&quot;:[&quot;application\/vnd.google-earth.kmz&quot;],
  &quot;kml&quot;:[&quot;application\/vnd.google-earth.kml+xml&quot;],&quot;rtx&quot;:[&quot;text\/richtext&quot;],
  &quot;rtf&quot;:[&quot;text\/rtf&quot;],&quot;jar&quot;:[&quot;application\/java-archive&quot;,&quot;application\/x-java-application&quot;,
  &quot;application\/x-jar&quot;],&quot;zip&quot;:[&quot;application\/x-zip&quot;,&quot;application\/zip&quot;,
  &quot;application\/x-zip-compressed&quot;,&quot;application\/s-compressed&quot;,&quot;multipart\/x-zip&quot;],
  &quot;7zip&quot;:[&quot;application\/x-compressed&quot;],&quot;xml&quot;:[&quot;application\/xml&quot;,&quot;text\/xml&quot;],
  &quot;svg&quot;:[&quot;image\/svg+xml&quot;],&quot;3g2&quot;:[&quot;video\/3gpp2&quot;],&quot;3gp&quot;:[&quot;video\/3gp&quot;,&quot;video\/3gpp&quot;],
  &quot;mp4&quot;:[&quot;video\/mp4&quot;],&quot;m4a&quot;:[&quot;audio\/x-m4a&quot;],&quot;f4v&quot;:[&quot;video\/x-f4v&quot;],&quot;flv&quot;:[&quot;video\/x-flv&quot;],
  &quot;webm&quot;:[&quot;video\/webm&quot;],&quot;aac&quot;:[&quot;audio\/x-acc&quot;],&quot;m4u&quot;:[&quot;application\/vnd.mpegurl&quot;],
  &quot;pdf&quot;:[&quot;application\/pdf&quot;,&quot;application\/octet-stream&quot;],
  &quot;pptx&quot;:[&quot;application\/vnd.openxmlformats-officedocument.presentationml.presentation&quot;],
  &quot;ppt&quot;:[&quot;application\/powerpoint&quot;,&quot;application\/vnd.ms-powerpoint&quot;,&quot;application\/vnd.ms-office&quot;,
  &quot;application\/msword&quot;],&quot;docx&quot;:[&quot;application\/vnd.openxmlformats-officedocument.wordprocessingml.document&quot;],
  &quot;xlsx&quot;:[&quot;application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;,&quot;application\/vnd.ms-excel&quot;],
  &quot;xl&quot;:[&quot;application\/excel&quot;],&quot;xls&quot;:[&quot;application\/msexcel&quot;,&quot;application\/x-msexcel&quot;,&quot;application\/x-ms-excel&quot;,
  &quot;application\/x-excel&quot;,&quot;application\/x-dos_ms_excel&quot;,&quot;application\/xls&quot;,&quot;application\/x-xls&quot;],
  &quot;xsl&quot;:[&quot;text\/xsl&quot;],&quot;mpeg&quot;:[&quot;video\/mpeg&quot;],&quot;mov&quot;:[&quot;video\/quicktime&quot;],&quot;avi&quot;:[&quot;video\/x-msvideo&quot;,
  &quot;video\/msvideo&quot;,&quot;video\/avi&quot;,&quot;application\/x-troff-msvideo&quot;],&quot;movie&quot;:[&quot;video\/x-sgi-movie&quot;],
  &quot;log&quot;:[&quot;text\/x-log&quot;],&quot;txt&quot;:[&quot;text\/plain&quot;],&quot;css&quot;:[&quot;text\/css&quot;],&quot;html&quot;:[&quot;text\/html&quot;],
  &quot;wav&quot;:[&quot;audio\/x-wav&quot;,&quot;audio\/wave&quot;,&quot;audio\/wav&quot;],&quot;xhtml&quot;:[&quot;application\/xhtml+xml&quot;],
  &quot;tar&quot;:[&quot;application\/x-tar&quot;],&quot;tgz&quot;:[&quot;application\/x-gzip-compressed&quot;],&quot;psd&quot;:[&quot;application\/x-photoshop&quot;,
  &quot;image\/vnd.adobe.photoshop&quot;],&quot;exe&quot;:[&quot;application\/x-msdownload&quot;],&quot;js&quot;:[&quot;application\/x-javascript&quot;],
  &quot;mp3&quot;:[&quot;audio\/mpeg&quot;,&quot;audio\/mpg&quot;,&quot;audio\/mpeg3&quot;,&quot;audio\/mp3&quot;],&quot;rar&quot;:[&quot;application\/x-rar&quot;,&quot;application\/rar&quot;,
  &quot;application\/x-rar-compressed&quot;],&quot;gzip&quot;:[&quot;application\/x-gzip&quot;],&quot;hqx&quot;:[&quot;application\/mac-binhex40&quot;,
  &quot;application\/mac-binhex&quot;,&quot;application\/x-binhex40&quot;,&quot;application\/x-mac-binhex40&quot;],
  &quot;cpt&quot;:[&quot;application\/mac-compactpro&quot;],&quot;bin&quot;:[&quot;application\/macbinary&quot;,&quot;application\/mac-binary&quot;,
  &quot;application\/x-binary&quot;,&quot;application\/x-macbinary&quot;],&quot;oda&quot;:[&quot;application\/oda&quot;],
  &quot;ai&quot;:[&quot;application\/postscript&quot;],&quot;smil&quot;:[&quot;application\/smil&quot;],&quot;mif&quot;:[&quot;application\/vnd.mif&quot;],
  &quot;wbxml&quot;:[&quot;application\/wbxml&quot;],&quot;wmlc&quot;:[&quot;application\/wmlc&quot;],&quot;dcr&quot;:[&quot;application\/x-director&quot;],
  &quot;dvi&quot;:[&quot;application\/x-dvi&quot;],&quot;gtar&quot;:[&quot;application\/x-gtar&quot;],&quot;php&quot;:[&quot;application\/x-httpd-php&quot;,
  &quot;application\/php&quot;,&quot;application\/x-php&quot;,&quot;text\/php&quot;,&quot;text\/x-php&quot;,&quot;application\/x-httpd-php-source&quot;],
  &quot;swf&quot;:[&quot;application\/x-shockwave-flash&quot;],&quot;sit&quot;:[&quot;application\/x-stuffit&quot;],&quot;z&quot;:[&quot;application\/x-compress&quot;],
  &quot;mid&quot;:[&quot;audio\/midi&quot;],&quot;aif&quot;:[&quot;audio\/x-aiff&quot;,&quot;audio\/aiff&quot;],&quot;ram&quot;:[&quot;audio\/x-pn-realaudio&quot;],
  &quot;rpm&quot;:[&quot;audio\/x-pn-realaudio-plugin&quot;],&quot;ra&quot;:[&quot;audio\/x-realaudio&quot;],&quot;rv&quot;:[&quot;video\/vnd.rn-realvideo&quot;],
  &quot;jp2&quot;:[&quot;image\/jp2&quot;,&quot;video\/mj2&quot;,&quot;image\/jpx&quot;,&quot;image\/jpm&quot;],&quot;tiff&quot;:[&quot;image\/tiff&quot;],
  &quot;eml&quot;:[&quot;message\/rfc822&quot;],&quot;pem&quot;:[&quot;application\/x-x509-user-cert&quot;,&quot;application\/x-pem-file&quot;],
  &quot;p10&quot;:[&quot;application\/x-pkcs10&quot;,&quot;application\/pkcs10&quot;],&quot;p12&quot;:[&quot;application\/x-pkcs12&quot;],
  &quot;p7a&quot;:[&quot;application\/x-pkcs7-signature&quot;],&quot;p7c&quot;:[&quot;application\/pkcs7-mime&quot;,&quot;application\/x-pkcs7-mime&quot;],&quot;p7r&quot;:[&quot;application\/x-pkcs7-certreqresp&quot;],&quot;p7s&quot;:[&quot;application\/pkcs7-signature&quot;],&quot;crt&quot;:[&quot;application\/x-x509-ca-cert&quot;,&quot;application\/pkix-cert&quot;],&quot;crl&quot;:[&quot;application\/pkix-crl&quot;,&quot;application\/pkcs-crl&quot;],&quot;pgp&quot;:[&quot;application\/pgp&quot;],&quot;gpg&quot;:[&quot;application\/gpg-keys&quot;],&quot;rsa&quot;:[&quot;application\/x-pkcs7&quot;],&quot;ics&quot;:[&quot;text\/calendar&quot;],&quot;zsh&quot;:[&quot;text\/x-scriptzsh&quot;],&quot;cdr&quot;:[&quot;application\/cdr&quot;,&quot;application\/coreldraw&quot;,&quot;application\/x-cdr&quot;,&quot;application\/x-coreldraw&quot;,&quot;image\/cdr&quot;,&quot;image\/x-cdr&quot;,&quot;zz-application\/zz-winassoc-cdr&quot;],&quot;wma&quot;:[&quot;audio\/x-ms-wma&quot;],&quot;vcf&quot;:[&quot;text\/x-vcard&quot;],&quot;srt&quot;:[&quot;text\/srt&quot;],&quot;vtt&quot;:[&quot;text\/vtt&quot;],&quot;ico&quot;:[&quot;image\/x-icon&quot;,&quot;image\/x-ico&quot;,&quot;image\/vnd.microsoft.icon&quot;],&quot;csv&quot;:[&quot;text\/x-comma-separated-values&quot;,&quot;text\/comma-separated-values&quot;,&quot;application\/vnd.msexcel&quot;],&quot;json&quot;:[&quot;application\/json&quot;,&quot;text\/json&quot;]}';
      $all_mimes = json_decode($all_mimes,true);
      foreach ($all_mimes as $key =&gt; $value) {
          if(array_search($mime,$value) !== false) return $key;
      }
      return false;
  }
  
  function getRequestHeaders() {
      $headers = array();
      foreach($_SERVER as $key =&gt; $value) {
          if (substr($key, 0, 5) &lt;&gt; 'HTTP_') {
              continue;
          }
          $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
          $headers[$header] = $value;
      }
      return $headers;
  }
  
  
  ?&gt; 
  </code></pre>