Skip to content

PHP FILE UPLOAD #2

@aaronperkel

Description

@aaronperkel

PLEASE HELP, we are trying to upload images to our site using html and php but its not working.

We have
<form action="add_item.php" id="newItem" method="POST" enctype="multipart/form-data">
and
<input type="file" id="image_file" name="image_file" accept="image/*" required>

and also in add_item.php:

$image_path = "";
if (isset($_FILES['image_file']) && $_FILES['image_file']['error'] == UPLOAD_ERR_OK) {
    $targetDir = "public/images/";
    if (!is_dir($targetDir)) {
        mkdir($targetDir, 0777, true);
    }
    $fileName = basename($_FILES['image_file']['name']);
    $targetFilePath = $targetDir . $fileName;
    
    if (move_uploaded_file($_FILES['image_file']['tmp_name'], $targetFilePath)) {
        $image_path = "./public/images/" . $fileName;
    } else {
        $image_path = ""; // Upload failed, you might want to handle this case differently
    }
} else {
    // No file was uploaded or an error occurred
    $image_path = "./public/images/default.png";
}

but no file is uploading, and also, the path is not getting grabbed from file name

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions