在2个文件中的相同代码,但其中一个不工作,上传图像到服务器PHP [关闭]

4jb9z9bj  于 9个月前  发布在  PHP
关注(0)|答案(1)|浏览(95)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
5天前关闭。
Improve this question
这是我上传图片到服务器的代码。相同的代码用于2个页面(例如A.php和B.php)。下面的php代码在A.php中,在B.php中唯一的区别是$target_dir。B.php可以处理这些代码,但是由于某种原因,A.php总是失败if语句并回显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正在处理img预览。

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

qoefvg9y1#

刚刚在html上找到了罪魁祸首,错过了enctype="multipart/form-data"

相关问题