0

it was working fine on live host earlier but now i updated my php version to 8.1 and stopped working . But when i check it on xampp local server it is working fine .

my HTML code is :-

      <form method="POST" id="name-form" enctype="multipart/form-data">
            <div class="h4">Update Display</div>
            <input type='file' name="dp" class="input-form" accept="image/*">
            <input type="submit" value="Update" name="dp" class="edit-button">
            </form>

and php is :-

      function compressImage($source, $destination, $quality){
     $imgInfo = getimagesize($source);
     $dataMine = $imgInfo['mime'];

    switch ($dataMine) {
    case 'image/jpeg':
        $image = imagecreatefromjpeg($source);
        break;
    case 'image/png':
        $image = imagecreatefrompng($source);
        break;
    case 'image/jpg':
        $image = imagecreatefromjpeg($source);
        break;
    default:
        $image = imagecreatefromjpeg($source);
}

   imagejpeg($image, $destination, $quality);

   // final Return compressed image 
     return $destination;
    }
      if (isset($_POST['dp'])) {
      $filename = $_FILES["dp"]["name"];
      $tempname = $_FILES["dp"]["tmp_name"];
      $imgsize = $_FILES["dp"]["size"];
      $folder = "covers/" . time() . $filename;
      $fold = "covers/" . time() . $filename;
      $allowed = array('jpeg', 'png', 'jpg');
      $ext = pathinfo($folder, PATHINFO_EXTENSION);
        if (!in_array($ext, $allowed)) {
      $errors['DP'] = "Sorry, only JPG, JPEG, PNG  files are allowed.";
         }
      if ($imgsize > 3098152) {
      $errors['size'] = "File size must be less than 3MB";
      }
      if (count($errors) === 0) {
      $compressedImage = compressImage($tempname, $folder, 40);
      if ($compressedImage) {
      move_uploaded_file($compressedImage, $folder);

    $pd = ("update inst set cover = :folder where uid = :uid limit 1 ");
    $result_to_update_dp = $conn->prepare($pd);
    $result_to_update_dp->bindParam(':folder', $fold, PDO::PARAM_STR);
    $result_to_update_dp->bindParam(':uid', $uid, PDO::PARAM_STR);
    $dp_update = $result_to_update_dp->execute();
    if ($dp_update) {
        $_SESSION['success'] = "Profile Picture Updated Successfully. May Reflect After Some Time";
        $uploadDir = "covers/";
        //$_SERVER['DOCUMENT_ROOT']. 
        $uploadFile = $uploadDir . basename(time() . $filename);
        $uploadRequest = array(
            'fileName' => basename($uploadFile),
            'fileData' => base64_encode(file_get_contents($uploadFile))
        );
        header("location:index.php");
        exit();
        }
        }
     } else {
     $errors['upload'] = "OOps Something Wrong ! Try Again";
        }
       }

it is keep saying Warning: Undefined Array Key "Dp" In /Home/Myclass2/Public_html/User.Php On Line 403

Warning: Trying To Access Array Offset On Value Of Type Null In /Home/Myclass2/Public_html/User.Php On Line 403

Warning: Undefined Array Key "Dp" In /Home/Public_html/User.Php On Line 404

Warning: Trying To Access Array Offset On Value Of Type Null In /Home/Myclass2/Public_html/User.Php On Line 404

Warning: Undefined Array Key "Dp" In /Home/Public_html/User.Php On Line 405

Warning: Trying To Access Array Offset On Value Of Type Null In /Home//Public_html/User.Php On Line 405

But it is working fine localhost .

thanks in advance !

Bittu
  • 51
  • 1
  • 7
  • Try `$_POST['dp']` to `$_POST['Dp']`. Error messages, it is showing `"Dp" `with a capital `"D"`. – iamafish Mar 03 '23 at 14:30
  • 1
    thanks bro it is due to $errors['DP'] – Bittu Mar 03 '23 at 14:33
  • 2
    I would be surprised if this had anything to do with the version upgrade in reality. Probably something else changed around the same time (e.g. in your code) – ADyson Mar 03 '23 at 14:40
  • 1
    but it was working fine on localhost – Bittu Mar 03 '23 at 14:45
  • 1
    Are you sure both versions of the PHP and HTML were identical? AFAIK PHP8 isn't any stricter about case-based string matching – ADyson Mar 03 '23 at 14:46
  • I find this strange too. Each error sentence starts with a capital letter including path. – iamafish Mar 03 '23 at 14:46
  • i have checked it with php 8.0 its working fine but with 8.1 its showing these errors at same server with same script . why ? – Bittu Mar 05 '23 at 01:35

1 Answers1

1

You can try changing all instances of $_POST['dp'] to $_POST['Dp']. Error messages, it is showing "Dp" with a capital "D"

if (isset($_POST['Dp'])) {
    // rest of the code
}
iamafish
  • 817
  • 3
  • 13
  • 24