Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Multi-file WordPress uploads from the front-end
<?php /* This function attaches the image to the post in the database, add it to functions.php */
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
} ?>
<?php /* Also in the template file, this script processes any $_POST data and hands off the images to the insert_attachment() function. Again you may want to wrap this in a current_user_can conditional, and probably do some sanity checks on incoming $_POST data. */
global $post;
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}
}
}
} ?>
<!-- This is the form that you add to the template file. You probably want to wrap this in a current_user_can conditional. -->
<form action="#" method="post" enctype="multipart/form-data"><input type="file" name="upload_attachment[]" /> <input type="submit" value="Upload" /></form>
@madebyreformat

This comment has been minimized.

Show comment Hide comment
@madebyreformat

madebyreformat Feb 27, 2012

Dude you have really helped me out here, i've been scratching my head for an hour. Also i added this to check whether it's an image or not

foreach ($_FILES as $file => $array) {
    if(getimagesize($array['tmp_name'])){
        $newupload = insert_attachment($file,$pid);
    }else{
        // Wrong file type
    }
}

Dude you have really helped me out here, i've been scratching my head for an hour. Also i added this to check whether it's an image or not

foreach ($_FILES as $file => $array) {
    if(getimagesize($array['tmp_name'])){
        $newupload = insert_attachment($file,$pid);
    }else{
        // Wrong file type
    }
}
@varadha

This comment has been minimized.

Show comment Hide comment
@varadha

varadha Jun 22, 2013

it's not working for me.

help to use it for my project

varadha commented Jun 22, 2013

it's not working for me.

help to use it for my project

@enjoydevs

This comment has been minimized.

Show comment Hide comment
@enjoydevs

enjoydevs Jul 9, 2013

Thanks!!!

For show:

'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $current_post, // check you print ID post )); if ( !$attachments ) echo "No images"; else { foreach ( $attachments as $attachment ) { the_attachment_link( $attachment->ID , false ); apply_filters( 'the_title' , $attachment->post_title ); } } ?>

Thanks!!!

For show:

'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $current_post, // check you print ID post )); if ( !$attachments ) echo "No images"; else { foreach ( $attachments as $attachment ) { the_attachment_link( $attachment->ID , false ); apply_filters( 'the_title' , $attachment->post_title ); } } ?>
@gulzarahmed

This comment has been minimized.

Show comment Hide comment
@gulzarahmed

gulzarahmed Feb 11, 2014

Thanks a lot!!

Thanks a lot!!

@darnin

This comment has been minimized.

Show comment Hide comment
@darnin

darnin May 11, 2014

Thank you for your sharing, it works!

darnin commented May 11, 2014

Thank you for your sharing, it works!

@DLHC

This comment has been minimized.

Show comment Hide comment
@DLHC

DLHC Jun 28, 2014

Hi,i want to use this as attach images in comment.

I put this in comments-review.php


And this into functions.php


And this into comments-listing.php

$value) { if ($files['name'][$key]) { $file = array( 'name' => $files['name'][$key], 'type' => $files['type'][$key], 'tmp_name' => $files['tmp_name'][$key], 'error' => $files['error'][$key], 'size' => $files['size'][$key] ); ``` $_FILES = array("upload_attachment" => $file); foreach ($_FILES as $file => $array) { $newupload = insert_attachment($file,$post->ID); } } } ``` } ?>

But how to show the images ???

DLHC commented Jun 28, 2014

Hi,i want to use this as attach images in comment.

I put this in comments-review.php


And this into functions.php


And this into comments-listing.php

$value) { if ($files['name'][$key]) { $file = array( 'name' => $files['name'][$key], 'type' => $files['type'][$key], 'tmp_name' => $files['tmp_name'][$key], 'error' => $files['error'][$key], 'size' => $files['size'][$key] ); ``` $_FILES = array("upload_attachment" => $file); foreach ($_FILES as $file => $array) { $newupload = insert_attachment($file,$post->ID); } } } ``` } ?>

But how to show the images ???

@nicleg

This comment has been minimized.

Show comment Hide comment
@nicleg

nicleg Jul 1, 2014

Great and thank's for sharing.

A question still in my mind.
Why do you use :

        ...
        $_FILES = array("upload_attachment" => $file);

        foreach ($_FILES as $file => $array) {
           $newupload = insert_attachment($file,$post->ID);
        }
        ...

And why (I've tested) just the code below is not enougth ?

        ...
        //$_FILES = array("upload_attachment" => $file);

        //foreach ($_FILES as $file => $array) {
           $newupload = insert_attachment($file,$post->ID);
        //}
        ...

To explain deeper, why do you override $_FILES variable. And why the insert_attachment($file,$post->ID); is inside a foreach ?

Is the first foreach too fast for the insert_attachment ?

nicleg commented Jul 1, 2014

Great and thank's for sharing.

A question still in my mind.
Why do you use :

        ...
        $_FILES = array("upload_attachment" => $file);

        foreach ($_FILES as $file => $array) {
           $newupload = insert_attachment($file,$post->ID);
        }
        ...

And why (I've tested) just the code below is not enougth ?

        ...
        //$_FILES = array("upload_attachment" => $file);

        //foreach ($_FILES as $file => $array) {
           $newupload = insert_attachment($file,$post->ID);
        //}
        ...

To explain deeper, why do you override $_FILES variable. And why the insert_attachment($file,$post->ID); is inside a foreach ?

Is the first foreach too fast for the insert_attachment ?

@craigiswayne

This comment has been minimized.

Show comment Hide comment
@craigiswayne

craigiswayne Oct 5, 2016

Greatly appreciate this!

Greatly appreciate this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment