PHP图片上传仅更新数据库成功 未在uploads目录生成文件如何解决
图片未生成到指定目录的问题修正
核心问题原因
- 表单缺失文件上传必需的属性。当前
<form>标签没有声明enctype="multipart/form-data",浏览器默认的表单编码格式不会携带选中的文件内容上传,PHP端无法接收到文件临时路径,$_FILES数组为空,根本不会触发文件移动逻辑。 - 存储路径与需求不匹配,且未做目录校验。代码中写死的上传目录是
uploads/,没有指向你需要的uploads/IMAGE目录,同时未校验该目录是否存在、是否具备PHP进程的写入权限,就算文件成功上传,也会因路径不存在或无权限导致移动失败。 - 文件名处理逻辑错误。代码对生成的存储文件名直接执行了
urlencode(),生成的文件名会携带大量特殊转义字符,容易触发文件系统的合法名校验、写入权限校验失败。 - 无效逻辑冗余。代码开头
$artist_photo = $_POST['artist_photo'];这行没有实际意义,你的HTML表单中没有同名字的文本输入项,获取到的值始终为空,容易造成逻辑混淆。 - 变量拼写手误。
$destinatin为拼写错误的变量名(正确拼写为destination),虽不影响当前逻辑,但后续维护极易引发问题。
修正后的实现代码
修正后的HTML表单部分
只需要在form标签补上文件上传必需的属性即可:
<form action="#" method="post" enctype="multipart/form-data"> <!-- 原有表单内容保持不变 --> <div class="form-group"> <label for="artist_id">Artist ID</label> <input type="text" name="artist_id" class="form-control" id="artist_id" placeholder="Enter artist id"> </div> <div class="form-group"> <label for="artist_name">Artist Name</label> <input type="text" name="artist_name" class="form-control" id="artist_name" placeholder="Enter artist name"> </div> <div class="form-group"> <label for="artist_id">Artist Biography</label> <input type="text" name="artist_biography" class="form-control" id="artist_biography" placeholder="Enter artist biography"> </div> <label for="artist_photo">Artist Photo</label> <div style="margin-bottom: 25px;" class="file-upload"> <label style="background-image: linear-gradient(to right, rgb(240, 152, 25) 0%, rgb(237, 222, 93) 51%, rgb(240, 152, 25) 100%);" for="upload" class="file-upload__label">Upload Photo Artist to <?php echo $wo['config']['msc_site_name']; ?> Music</label> <input type="file" accept=".png,.jpg,.jpeg,.gif,image/*" name="artist_photo" class="file-upload__input" id="artist_photo"> </div> <img style="text-align: initial;display: none;width: 170px;border-radius: 8px;" id="blah" src="#" alt="your image" /> <div class="form-group"> <label for="artist_id">Artist Social Facebook</label> <input type="text" name="artist_social_fb" class="form-control" id="artist_social_fb" placeholder="Enter artist name"> </div> <div class="form-group"> <label for="artist_id">Artist Social Instagram</label> <input type="text" name="artist_social_ig" class="form-control" id="artist_social_ig" placeholder="Enter artist name"> </div> <div class="form-group"> <label for="artist_id">Artist Social Twitter</label> <input type="text" name="artist_social_tw" class="form-control" id="artist_social_tw" placeholder="Enter artist name"> </div> <button type="submit" name="update" class="btn btn-primary m-t-15 waves-effect">Update Song</button> </form>
修正后的PHP逻辑
<?php if(isset($_POST['update'])) { $hostname = "localhost"; $username = "root"; $password = ""; $databaseName = "hmusic"; $connect = mysqli_connect($hostname, $username, $password, $databaseName); // 移除原本无意义的$_POST取值,先初始化图片变量做兜底 $artist_photo = ''; if(isset($_FILES['artist_photo']['error']) && $_FILES['artist_photo']['error'] == 0){ // 改为指向需求的存储目录,末尾加斜杠避免路径拼接错误 $target_dir = "uploads/IMAGE/"; // 目录不存在时自动创建,权限设为0755保证可写 if(!is_dir($target_dir)) { mkdir($target_dir, 0755, true); } // 生成唯一文件名,去掉不合理的urlencode处理,保留原文件后缀 $file_ext = pathinfo($_FILES["artist_photo"]["name"], PATHINFO_EXTENSION); $artist_photo = time()."_".rand(100000,10000000).rand(100000,10000000).".".$file_ext; $artist_photo = str_replace(" ", "_", $artist_photo); $source = $_FILES["artist_photo"]["tmp_name"]; $destination = $target_dir . $artist_photo; // 修正原本拼写错误的变量名 if(move_uploaded_file($source, $destination)){ // 上传成功,保留生成的文件名存入数据库 }else{ // 上传失败时置空,可在此处加错误日志打印排查原因 $artist_photo = ""; } } $artist_id = $_POST['artist_id']; $artist_name = $_POST['artist_name']; $artist_biography = $_POST['artist_biography']; $artist_social_fb = $_POST['artist_social_fb']; $artist_social_ig = $_POST['artist_social_ig']; $artist_social_tw = $_POST['artist_social_tw']; // 补转义逻辑,规避基础的SQL注入风险 $query = " UPDATE artist SET artist_name = '".mysqli_real_escape_string($connect, $artist_name)."' , artist_biography = '".mysqli_real_escape_string($connect, $artist_biography)."' , artist_social_fb = '".mysqli_real_escape_string($connect, $artist_social_fb)."' , artist_social_ig = '".mysqli_real_escape_string($connect, $artist_social_ig)."' , artist_social_tw = '".mysqli_real_escape_string($connect, $artist_social_tw)."' , artist_photo = '".mysqli_real_escape_string($connect, $artist_photo)."' WHERE artist_id = ".intval($artist_id)." "; $result = mysqli_query($connect, $query); if($result) { echo 'Data Updated'; }else{ echo 'Data Not Updated'; } mysqli_close($connect); } ?>
额外排查提示
- 确认
uploads/IMAGE目录的属主和PHP运行用户一致,避免因权限不足导致文件写入失败 - 如果上传超过2M的文件失败,需要调整
php.ini中的upload_max_filesize和post_max_size配置
内容的提问来源于stack exchange,提问作者Tio Uzumaki
相关产品推荐
相关产品推荐

