• Resolved astinne

    (@astinne)


    I am “permanently deleting” from the media library as the administrator. When I permanently delete the file, it remains in my “wp-content/uploads” folder. Doing a google search I found two sources talking about this. Both of the sources blame a plugin called “WPML” that does language translations.

    I am not using this plugin. The files will not delete from the system using the “delete permanently” function in the media library. The only plugin I had affecting the media library was FileBird Lite but I see no forums blaming this plugin.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Just deactivate the plugin(s) you suspect and then test whether deleting them is more successful. It may also help to deactivate all plugins and then activate them individually after each new test. WordPress without any plugins deletes the files from the upload directory as requested.

    Thread Starter astinne

    (@astinne)

    Okay I understand where there is an issue now.

    I added a sample.pdf to the media library and deleted it. It got rid of it from the uploads folder.

    I have a PHP shortcode that gives a file upload form and then submits it to the media library.

    I am unable to delete from library and upload folder for files that I added through my PHP shortcode. (Everything else about it is working fine.) Should I ask about this on a different forum then?

    You can also show your source code here.

    Thread Starter astinne

    (@astinne)

    HTML that lets user enter files on frontend:

    <html>
        <body>
            <form id="upload" enctype="multipart/form-data" method="post">
            <input id="fileupload" name="myfile" type="file" />
            <input type="submit" value="Submit" id="submit" />
       <?php
            $db = new wpdb('username','password','db','localhost');
            if( ! empty( $_FILES ) ){
                $filename = $_FILES['myfile']['name']; 
                $file_url = "https://website-name/wp-content/uploads/$filename";
                $url_exists = is_200($file_url);
                $sql = "INSERT INTO column (name, url) VALUES ('$filename', '$file_url');";
    
                if($url_exists){
                    echo "This file already exists within the database.";
                } else{
                    foreach( $_FILES as $file ) {
                        if( is_array( $file ) ) {
                        $filename = $_FILES['myfile']['name']; 
                        $file_url = "https://website-name/wp-content/uploads/$filename";
                        $attachment_id = upload_user_file( $file );
                        $db->query($sql);
                        echo "Your file has been added.";
                        }
                    }
                }
            }
    ?>
            </form>
        </body>
    </html>

    PHP Function to upload files to media library:

    <?php
    function upload_user_file( $file = array() ) {
    
    	require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    
          $file_return = wp_handle_upload( $file, array('test_form' => false ) );
    
          if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
              return false;
          } else {
    
              $filename = $file_return['file'];
    
              $attachment = array(
                  'post_mime_type' => $file_return['type'],
                  'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                  'post_content' => '',
                  'post_status' => 'inherit',
                  'guid' => $file_return['url']
              );
    
              $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
    
              require_once(ABSPATH . 'wp-admin/includes/image.php');
              $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
              wp_update_attachment_metadata( $attachment_id, $attachment_data );
    
              if( 0 < intval( $attachment_id ) ) {
              	return $attachment_id;
              }
          }
    
          return false;
    }
    ?>

    It works, but when I attempt to delete the files added with this method by using the media libary, the file still remains in the upload folder. (Sorry for late reply.)

    It actually looks OK to me. Unfortunately, I can’t manage to test it in a short time because I don’t have the function to call.

    Nevertheless, 2 tips:
    Check whether you get an error message in the error.log when you try to delete a file uploaded in this way.
    Have a look in the database at the created record of such a file (table posts, post_type => ‘attachment’) and compare it with other attachments that are uploaded in the normal way. My guess is that there is a difference here that is decisive for this. Unfortunately, I have no idea which one it could be.

    Thread Starter astinne

    (@astinne)

    I didn’t even know they were titled as attachments. I’ll look that up. Thank you for your time.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Permanently deleting from media library not deleting from wp-content/uploads’ is closed to new replies.