早些时候它在实时主机上运行良好,但现在我将我的 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){
$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";
}
}
一直在说 警告: Undefined Array Key "Dp" In /Home/Myclass2/Public_html/User.Php On Line 403
警告: 尝试访问 /Home/Myclass2/Public_html/User.Php 中类型为 Null 的值的数组偏移 403
警告:/Home/Public_html/User.Php 404 行中未定义的数组键“Dp”
警告: 尝试访问 /Home/Myclass2/Public_html/User.Php 中类型为 Null 的值的数组偏移 404
警告:405 行 /Home/Public_html/User.Php 中未定义的数组键“Dp”
警告: 尝试在 /Home//Public_html/User.Php 405 行中访问空值类型的数组偏移量
但是本地主机运行良好。
提前致谢!
您可以尝试将
$_POST['dp']
的所有实例更改为$_POST['Dp']
。错误信息,它显示的是大写字母“D”的“Dp”
if (isset($_POST['Dp'])) {
// rest of the code
}