Make WordPress Core

Opened 21 months ago

Last modified 17 months ago

#58787 new defect (bug)

when upload it multi upload duplicate the files in the media

Reported by: wasimo's profile wasimo Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.2.2
Component: Upload Keywords:
Focuses: Cc:

Description (last modified by sabernhardt)

<?php
// Make sure this code is executed within WordPress environment
if (!defined('ABSPATH')) {
    exit;
}

if (isset($_POST['submit'])) {
    // Check for a file upload
    if (isset($_FILES['filed'])) {
        // Upload the file
        require_once ABSPATH . 'wp-admin/includes/image.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/media.php';

        $post_id = 0; // Change this to the ID of the post you want to attach the file to

        // Check if a file was selected
        if ($_FILES['filed']['error'] !== UPLOAD_ERR_NO_FILE) {
            // This will upload the file and attach it to the given post
            $attachment_id = media_handle_upload('filed', $post_id);

            if (is_wp_error($attachment_id)) {
                // There was an error uploading the file
                $response = array('error' => $attachment_id->get_error_message());
            } else {
                // The file was uploaded successfully
                $response = array('success' => 'File uploaded successfully with attachment ID ' . $attachment_id);
            }
        } else {
            // No file was selected
            $response = array('error' => 'No file selected.');
        }

        // Send the response back to the client-side script
        echo json_encode($response);
    }

    // IMPORTANT: Don't forget to exit at the end of your callback function
    exit;
}
?>

<form method="POST" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">
    <input type="file" name="filed" id="file">
    <input type="submit" name="submit" value="Upload">
</form>

<?php die(); ?>

Change History (1)

#1 @sabernhardt
17 months ago

  • Component changed from General to Upload
  • Description modified (diff)
Note: See TracTickets for help on using tickets.