[wp-trac] [WordPress Trac] #58787: when upload it multi upload duplicate the files in the media

WordPress Trac noreply at wordpress.org
Mon Oct 30 19:36:30 UTC 2023


#58787: when upload it multi upload duplicate the files in the media
--------------------------+------------------------------
 Reporter:  wasimo        |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Upload        |     Version:  6.2.2
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------
Changes (by sabernhardt):

 * component:  General => Upload


Old description:

> <?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(); ?>

New description:

 {{{
 <?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(); ?>
 }}}

--

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58787#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list