图片上传功能报错排查:空文件提交时自定义错误无法正常触发
解决方案:正确处理无文件上传的情况
你的问题根源在于没有先判断用户是否实际选择了文件,就直接调用getimagesize处理空的临时文件路径,从而触发系统警告。之前添加的检查逻辑可能有误,导致正常上传流程被阻断。下面是修复后的完整代码,以及关键修改说明:
关键修改点
- 增加了对
$_FILES['file']['error']的判断:通过UPLOAD_ERR_NO_FILE识别用户未选择文件的情况,直接返回自定义错误;用UPLOAD_ERR_OK确保文件正常上传后再执行后续逻辑。 - 将文件有效性检查放在所有处理逻辑的最前面,避免空值进入后续代码引发报错。
- 优化了错误提示的输出逻辑,确保用户能看到清晰的自定义提示,而非系统警告。
修复后的完整代码
<?php // UPLOAD IMAGE if(isset($_POST["UploadImage"])) { // 首先检查是否有文件被上传 if($_FILES['file']['error'] === UPLOAD_ERR_NO_FILE) { // 未选择文件时显示自定义错误 $msg = "<div class=\"alert alert-danger\">请选择要上传的图片文件</div>"; } elseif($_FILES['file']['error'] === UPLOAD_ERR_OK && is_array($_FILES)) { $file = $_FILES['file']['tmp_name']; $sourceProperties = getimagesize($file); // 检查是否为有效图片(避免非图片文件被上传) if(!$sourceProperties) { $msg = "<div class=\"alert alert-danger\">请上传有效的图片文件(JPG、PNG、GIF)</div>"; exit; } $fileNewName = time(); $folderPath = "../userImages/"; $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $imageType = $sourceProperties[2]; $resized = "_resized"; $line = "_"; $original = "_original"; switch ($imageType) { case IMAGETYPE_PNG: $imageResourceId = imagecreatefrompng($file); $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]); imagepng($targetLayer,$folderPath.$RegID.$line.$fileNewName.$resized. "." .$ext); break; case IMAGETYPE_GIF: $imageResourceId = imagecreatefromgif($file); $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]); imagegif($targetLayer,$folderPath.$RegID.$line.$fileNewName.$resized. "." .$ext); break; case IMAGETYPE_JPEG: $imageResourceId = imagecreatefromjpeg($file); $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]); imagejpeg($targetLayer,$folderPath.$RegID.$line.$fileNewName.$resized. "." .$ext); break; default: $msg = "<div class=\"alert alert-danger\">Wrong File Format - Only JPG, PNG or GIF - Max 2 MB</div>"; exit; break; } $file = @imagecreatefromjpeg($folderPath.$RegID.$line.$fileNewName.$resized. "." .$ext); if (!$file) { $file= imagecreatefromstring(file_get_contents($folderPath.$RegID.$line.$fileNewName.$resized. "." .$ext)); } // POST TO DATABASE $RegPhoto = $folderPath.$RegID.$line.$fileNewName.$resized. "." .$ext; // 优化SQL语句:RegID作为WHERE条件,无需在SET中重复赋值 $sql = "UPDATE registered SET RegPhoto = ? WHERE RegID = ?"; $stmt = $connect->prepare($sql); $stmt->bind_param('si', $RegPhoto, $RegID ); $stmt->execute(); $msg = "<div class=\"alert alert-success\">Updated Profile Successfully</div>"; } else { // 处理其他上传错误(比如文件过大、服务器错误等) $msg = "<div class=\"alert alert-danger\">文件上传失败,请重试</div>"; } } function imageResize($imageResourceId,$width,$height) { $targetWidth =220; $targetHeight =200; $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight); imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height); return $targetLayer; } ?>
额外优化提示
- 我帮你修正了SQL语句的冗余问题:
RegID作为WHERE条件,不需要在SET中再次赋值,减少了不必要的数据库操作。 - 可以进一步添加文件大小检查,确保上传文件不超过你设置的2MB限制,比如通过
$_FILES['file']['size'] < 2*1024*1024来判断。
内容的提问来源于stack exchange,提问作者CwStrange
相关产品推荐
相关产品推荐

