0

All, I'm using a HTML5 uploader to upload some images. This is part of the file that sends the data to my PHP processing script:

var ajax    = $.ajax({
    url:        ajaxurl,
    //dataType:     options.ajaxDataType || 'json',
    type:       options.ajaxType || 'POST',
    cache:      false,
    data:       { name: $file.data('filename'), filename: $file.data('file'), file: evt.target.result, data: options.data, action: 'test_upload' },
    headers:    options.ajaxHeaders,

From the uploader they process the file upload with this code:

if (!empty($_POST)) {
    $error                  = false;
    $absolutedir            = dirname(__FILE__);
    $dir                    = '/tmp/';
    $serverdir              = $absolutedir.$dir;
    $tmp                    = explode(',',$_POST['file']);
    $file                   = base64_decode($tmp[1]);
    $extension              = strtolower(end(explode('.',$_POST['filename'])));
    $filename               = $_POST['name'].'.'.$extension;
    //$filename             = $file.'.'.substr(sha1(time()),0,6).'.'.$extension;
    $handle                 = fopen($serverdir.$filename,'w');
    fwrite($handle, $file);
    fclose($handle);
    $response = array(
            "result"        => true,
            "url"           => $dir.$filename.'?'.time(), //added the time to force update when editting multiple times
            "filename"      => $filename
    );
    echo json_encode($response);
    //echo json_encode(array('result'=>true));
}

However, I would like Wordpress to handle my file upload so I'm trying to use the following function to handle my file upload:

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

    $filename = $_POST['filename']; 
    $attachment_id = media_handle_upload( $filename, '1' );
    print_r($attachment_id);

}

I'm trying to pass the filename because the media_handle_upload codex for the first argument (in my example $filename) says this: Index into the $_FILES array of the upload

How can I successfully use WP to upload this file using AJAX to pass me a POST variable instead of a $_FILES variable?

EDIT: When I currently do it this way I get a WP_ERROR saying the file is empty. I created a pastebin with the JS code to handle the file processing form so you can see where the file names are coming from. http://pastebin.com/Shc4KqX1

Here is the file input:

<input type="file" name="file" />
user1048676
  • 9,756
  • 26
  • 83
  • 120
  • XHR2 allows files to be uploaded via ajax as they would have been if you used a regular form, although I'm assuming you had a File/Blob object at some point in time – Musa Jan 09 '15 at 02:41
  • @Musa Not sure what that is. I did see this in the code `file = files[$file.data('index')];` – user1048676 Jan 09 '15 at 02:43
  • It allows you to upload files without having to convert them to strings, see http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery?rq=1 – Musa Jan 09 '15 at 02:48
  • @Musa I put a pastebin that shows the whole JS code on how it's handling the file input form. – user1048676 Jan 09 '15 at 02:54

1 Answers1

0
<input id="uploadBtn" type="file" class="upload" name="pimage">

Function media_handle_upload upload your image in wordpress

Use $filename = $_POST['filename']['temp_name']; not $filename = $_POST['filename'];

if ( !empty( $_FILES["pimage"]["name"] ) ) {
    $attachment_id = media_handle_upload( 'pimage', 0 );
}
jay.jivani
  • 1,560
  • 1
  • 16
  • 33
  • I tried to do what you said and I still got a zero response. I got an `Illegal offset` error for the `temp_name` and when I change my file input to the name you have and change my if statement to what you had it didn't enter in the `media_handle_upload` and went to an else statement for error handling. – user1048676 Jan 09 '15 at 04:51
  • i have used this code to upload image in wordpress & it's working that why i give you this suggestion – jay.jivani Jan 09 '15 at 05:25
  • Ok thanks. I've had it work in the past when I could submit the form and get the `$_FILES` values as you suggested. In this example however, I'm not actually submitting the form so how it's being processed I don't believe I have direct access to the `$_FILES` variable to do it this way. – user1048676 Jan 09 '15 at 05:30