$HTTP_RAW_POST_DATA and Wordpress media_handle_upload
Posted
by
BaronVonKaneHoffen
on Stack Overflow
See other posts from Stack Overflow
or by BaronVonKaneHoffen
Published on 2011-03-12T16:09:17Z
Indexed on
2011/03/12
16:10 UTC
Read the original article
Hit count: 239
Hi there!
So I'm making something for which users need to upload images from a canvas element (on the front end) using Wordpress's in-built Media Upload API.
I've successfully uploaded images with the API from a file input in a form:
<input type="file" name="async-upload" id="async-upload" size="40" />
...
<?php $new_attach_id = media_handle_upload( 'async-upload', $new_id ); ?>
and I've saved images from the canvas using my own script:
<script type="text/javascript">
...
var img = canvas.toDataURL("image/png");
...
ajax.send(img );
</script>
...
<?php
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
$filteredData=substr($imageData, strpos($imageData, ",")+1);
$unencodedData=base64_decode($filteredData);
$fp = fopen( 'saved_images/canv_save_test.png', 'wb' );
fwrite( $fp, $unencodedData);
...
?>
Problem is, Wordpress's media_handle_upload() only accepts an index to the $_FILES array for the upload.
So the question is: How can I pass the image data from HTTP_RAW_POST_DATA to it? Could I make the $_FILES['tmp-name'] point to it somehow? Could I use another script as an intermediate step somehow? Could I unleash the monkeys on the typewriter until they come up with the answer?
Any help very very much appreciated!
Thanks, Kane
© Stack Overflow or respective owner