0
votes

Im trying to creating my own custom image/file uploader on the front-end of my WordPress site. When I upload a file it gets to my database just fine but in the Media Library the file doesn't upload fully and when I check my wp-content/uploads folder nothing is there how do I correct this.

Image of file in Media Library unfinished upload file in Media Library

<form method="post" enctype="multipart/form-data">
     Select image to upload:
     <input type="file" name="img-upload" id="img-upload" />
     <input type="submit" value="Upload Image" name="submit">
</form>

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

$image = $_POST['img-upload'];
$upload_dir = wp_upload_dir();

$file_name = basename($image);
//$file_data = file_get_contents($file);

$file_dir = $upload_dir['basedir'] . "/" . basename($image);
$file_type = wp_check_filetype($image,null);

$attachment = array(
    'guid' => $file_dir,
    'post_mime_type' => $file_type['type'],
    'post_title' => preg_replace('/\\.[^.\\s]{3,4}$/', '', $image),
    'post_content' => '',
    'post_status' => 'inherit'
 );

$attach_id = wp_insert_attachment( $attachment, $image,0 );
require_once(ABSPATH . 'wp-admin/includes/image.php');

$attach_data = wp_generate_attachment_metadata($attach_id, $file_dir);
wp_update_attachment_metadata($attach_id, $attach_data);
2
can you provide the contents of print_r($_FILES);?user2914191
Nothing prints when I use var_dump or print_r for $_FILES @user2914191Henzly Meghie
find out why you're not getting anything in $_FILES. stackoverflow.com/questions/3586919/… and just so youre aware, $_FILES should only contain data when you submit a form and upload a file.user2914191

2 Answers

0
votes

You can try this code. It will work

<?php
    if(isset($_POST['submit'])){

    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    require_once( ABSPATH . 'wp-admin/includes/file.php' );

    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    $attachment_id = media_handle_upload( 'img-upload', '' );
     if ( is_wp_error( $attachment_id ) ) {
     $attachment_url="";
     } 
     else {
     $attachment_url=wp_get_attachment_url( $attachment_id );;
      }

      if(!empty($attachment_url)){     
     //do the stuff you want to
     }


    }

    ?>
0
votes

Upload an attachment from a form on the front end of the site.

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
    <input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />
    <input type="hidden" name="post_id" id="post_id" value="0" />
    <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
    <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>

The code to save the attachment:

<?php

// Check that the nonce is valid, and the user can edit this post.
if ( 
    isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
    && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
    && current_user_can( 'edit_post', $_POST['post_id'] )
) {
    // The nonce was valid and the user has the capabilities, it is safe to continue.

    // These files need to be included as dependencies when on the front end.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

} else {

    // The security check failed, maybe show the user an error.
}
?>

source: https://developer.wordpress.org/reference/functions/media_handle_upload/#user-contributed-notes