0
votes

In frontend I have created a form that use upload_user_file() to upload files from fronted theme and every image is stored fine in WordPress media library (well it seems like so).

So when I upload a file named test.jpg its relative thumbnails is created and visible in media library. test-150x150.jpg test-300x225.jpg test-1024x768.jpg

The problem I have is when I delete this image from the media library. Only the created thumbnails is deleted and test.jpg is left intact in the upload folder. If I upload this file directly from media library, and then delete it all files is deleted including test.jpg.

Heres the code that stores values in a database and also upload files to the medialibrary. Is there another WordPress function to use? I guess upload_user_file() is not storing image data correctly in database?

global $wpdb;
global $post;

//$table = 'wp_verk1_project'; //$post_slug=$post->post_name;
$table = $wpdb->prefix . "project_name_" . $post_slug=$post->post_name;
$data = array(
    'contributorname' => $_POST['yourname'],
    'email' => $_POST['email'],
    'telephone' => $_POST['telephone'],
    'description' => $_POST['description'],
    'date' => date('Y-m-d'),
    'time' => date('H:i:s'),
    'upload' => upload_user_file($_FILES['file']),
    'upload2' => upload_user_file($_FILES['file2']),
    'upload3' => upload_user_file($_FILES['file3']),
    'upload4' => upload_user_file($_FILES['file4']),
    'upload5' => upload_user_file($_FILES['file5']),
    'rate' => '0'
);
$format = array(
    '%s',
    '%s'
);

$success = $wpdb->insert($table, $data, $format);

if ($success) {
    header("Location: " . get_bloginfo('url') . "/thank-you/");
    exit();
}

edit_user_file():

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;
}

Kind regards Johan

1

1 Answers

1
votes

I manage to solve my own question.

I digged into the WordPress core-files.

Ireplaced the following line:

$attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );

with this:

$attachment_id = wp_insert_attachment( $attachment, $filename );

In wp_postmeta, meta_value for the upload was set to the complete url (http://sitenamen.com/wp-content/upload/2014/12/file.jpg) while it should be stored like this: 2014/12/file.jpg

Now all files is deleted.