英文:
php image upload is not working on live server with php 8.1 but working with 8.0
问题
在之前的实际主机上它正常运行,但现在我将PHP版本更新到8.1后它停止工作。但当我在xampp本地服务器上检查时,它正常工作。
我的HTML代码是:
<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>
PHP代码是:
function compressImage($source, $destination, $quality){
    // 函数体
}
if (isset($_POST['dp'])) {
    // 代码块
}
它一直提示:
警告: 未定义的数组键“Dp”在/Home/Myclass2/Public_html/User.Php的第403行。
警告: 尝试访问类型为空的值的数组偏移在/Home/Myclass2/Public_html/User.Php的第403行。
警告: 未定义的数组键“Dp”在/Home/Public_html/User.Php的第404行。
警告: 尝试访问类型为空的值的数组偏移在/Home/Myclass2/Public_html/User.Php的第404行。
警告: 未定义的数组键“Dp”在/Home/Public_html/User.Php的第405行。
警告: 尝试访问类型为空的值的数组偏移在/Home/Public_html/User.Php的第405行。
但在本地主机上它正常工作。
提前感谢!
英文:
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 !
答案1
得分: 1
你可以尝试将所有的$_POST['dp']替换为$_POST['Dp']。错误消息显示为“Dp”(大写“D”)。
if (isset($_POST['Dp'])) {
    // 代码的其余部分
}
英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论