PHP 8.1 上线服务器的图像上传出现问题,但在 8.0 版本上可以正常工作。

huangapple go评论52阅读模式
英文:

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 :-

      &lt;form method=&quot;POST&quot; id=&quot;name-form&quot; enctype=&quot;multipart/form-data&quot;&gt;
            &lt;div class=&quot;h4&quot;&gt;Update Display&lt;/div&gt;
            &lt;input type=&#39;file&#39; name=&quot;dp&quot; class=&quot;input-form&quot; accept=&quot;image/*&quot;&gt;
            &lt;input type=&quot;submit&quot; value=&quot;Update&quot; name=&quot;dp&quot; class=&quot;edit-button&quot;&gt;
            &lt;/form&gt;

and php is :-

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

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

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

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

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

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[&#39;dp&#39;]替换为$_POST[&#39;Dp&#39;]。错误消息显示为“Dp”(大写“D”)。

if (isset($_POST[&#39;Dp&#39;])) {
    // 代码的其余部分
}
英文:

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

if (isset($_POST[&#39;Dp&#39;])) {
    // rest of the code
}

huangapple
  • 本文由 发表于 2023年3月3日 22:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定