You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一份PHP图片上传代码A.php失效,B.php正常,求助排查

PHP图片上传问题排查

问题描述

我写了一套PHP图片上传代码,分别用在A.php和B.php两个页面,只有$target_dir的路径不同。B.php能正常运行,但A.php始终无法通过初始判断,输出“Error during file upload”,尝试输出$uploadError也没有返回结果。

相关代码

A.php 后端代码

// Get uploaded image
if (isset($_FILES["thumbnail"]) && $_FILES["thumbnail"]["error"] == UPLOAD_ERR_OK) {
    // thumbnail's folder
    $target_dir = "../thumbnail/" . $user['id'] . "/project/$title/";
    $target_file = $target_dir . basename($_FILES["thumbnail"]["name"]);

    $tempPath=$_FILES["thumbnail"]["tmp_name"];

    // Create the target directory if it doesn't exist
    if (!file_exists($target_dir)) {
        mkdir($target_dir, 0777, true); // Adjust permissions as needed
    }

    // Move the uploaded file to the specified directory
    if (move_uploaded_file($_FILES["thumbnail"]["tmp_name"], $target_file)) {
        echo "The file " . basename($_FILES["thumbnail"]["name"]) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
} else {
    echo "Error during file upload.";
    $uploadError = $_FILES["thumbnail"]["error"];
    switch ($uploadError) {
        case UPLOAD_ERR_OK:
            // File uploaded successfully
            break;
        case UPLOAD_ERR_INI_SIZE:
            echo "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
            break;
        case UPLOAD_ERR_FORM_SIZE:
            echo "The uploaded file exceeds the MAX_FILE_SIZE directive in the HTML form.";
            break;
        case UPLOAD_ERR_PARTIAL:
            echo "The uploaded file was only partially uploaded.";
            break;
        case UPLOAD_ERR_NO_FILE:
            echo "No file was uploaded.";
            break;
        case UPLOAD_ERR_NO_TMP_DIR:
            echo "Missing a temporary folder for storing uploaded files.";
            break;
        case UPLOAD_ERR_CANT_WRITE:
            echo "Failed to write the uploaded file to disk.";
            break;
        case UPLOAD_ERR_EXTENSION:
            echo "A PHP extension stopped the file upload.";
            break;
        default:
            echo "An unknown error occurred during file upload.";
    }
}

图片预览JS代码

document.addEventListener('DOMContentLoaded', function () {
document.getElementById('thumbnail').addEventListener('change', function () {
    const uploadArea = document.querySelector('.upload-area');
    const previewImg = document.querySelector('.preview-img');

    const file = this.files[0]; // Get the selected file
    console.log("here we goo");

    if (file) {
        console.log("imge selected");
        // Display the selected file's name in the upload-area
        uploadArea.innerHTML = `
    <img src="../icons/upload.png" alt="">
    <p>${file.name}</p>
    `;

        // Optionally, you can also display a preview of the selected image
        const reader = new FileReader();
        reader.onload = function (e) {
            previewImg.src = e.target.result;
        };
        reader.readAsDataURL(file);
    } else {
        console.log("noooooo");
        // If no file is selected, restore the default content
        uploadArea.innerHTML = `
    <img src="../icons/upload.png" alt="">
    <p>Click here or Drag and Drop Image Over Here</p>
`;
        previewImg.src = "../images/115x140.svg";
    }
});

HTML代码

<!-- Image Content -->
<div class="column">
    <div class="col-item">
        <label for="thumbnail">thumbnail</label>
        <input type="file" name="thumbnail" id="thumbnail">
        <div class="upload-area">
            <img src="../icons/upload.png" alt="">
            <p>Drag and Drop Image Over Here</p>
        </div>
    </div>
    <div class="col-item">
        <label class="preview-label">thumbnail preview</label>
        
        <img src="<?php if(!empty($user['thumbnail']))  echo "admin/" . $user['thumbnail']; else echo "../images/350x250.svg";?>" alt="" class="preview-img preview-img-project">
    </div>
</div>

排查与解决建议

  • 优先检查表单属性:确认提交A.php的HTML表单是否添加了enctype="multipart/form-data",没有这个属性的话,文件根本无法被PHP接收,这是文件上传的必要条件。同时检查表单的action属性是否正确指向A.php。
  • 调试$_FILES数组:在A.php的else分支开头添加var_dump($_FILES);,查看整个$_FILES的结构。如果里面没有thumbnail字段,说明前端没有把文件传过来;如果有但error不是UPLOAD_ERR_OK,再对应错误码排查。
  • 验证变量有效性:A.php中$target_dir用到了$user['id']和$title,先通过var_dump($user['id'], $title);确认这两个变量是否有有效值,空值可能导致后续路径问题,甚至间接影响上传流程。
  • 检查服务器配置:虽然B.php正常,但A.php所在环境可能有不同配置。检查php.ini中的upload_max_filesize、post_max_size是否足够大,upload_tmp_dir是否存在且有读写权限。
  • 权限调整:提前确认../thumbnail/目录的权限,确保服务器进程有读写权限,mkdir的0777权限可能被服务器的umask限制,可以尝试改为0755。

内容的提问来源于stack exchange,提问作者Amrin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:50:53