使用默认的wordpress函数wp_handle_upload请参考
http://codex.wordpress.org/Function_Reference/wp_handle_upload
这是我的示例代码:
上传.html
<form action="" enctype="multipart/form-data" id="form" method="post" name="form">
<div id="upload">
<input id="file" name="file" type="file">
</div>
<input id="submit" name="submit" type="submit" value="Upload">
</form>
<div id="detail">
<div id="preview" style="height:100px;width:100px; display:none">
<img id="previewimg" src="" style="height:100px;width:100px;">
<img id="deleteimg" src="<?php echo plugins_url( '/images/remove.png'__FILE__);?>">
</div>
<div id="message">
<?php include "Function /upload.php";?>
</div>
</div>
上传.php
<?php
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
require_once (ABSPATH . 'wp-includes/pluggable.php');
if (isset ( $_POST ['submit'] )) {
if (! function_exists ( 'wp_handle_upload' ))
require_once (ABSPATH . 'wp-admin/includes/file.php');
$uploadedfile = $_FILES ['file'];
if (! empty ( $uploadedfile ['name'] )) {
$upload_overrides = array (
'test_form' => false
);
$movefile = wp_handle_upload ( $uploadedfile, $upload_overrides );
if ($movefile) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
} else {
echo "Please select the image to upload";
}
}
?>
上传.js`
jQuery(document).ready(function() {
// Function for Preview Image.
jQuery(function() {
jQuery(":file").change(function() {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});
function imageIsLoaded(e) {
jQuery('#message').css("display", "none");
jQuery('#preview').css("display", "block");
jQuery('#previewimg').attr('src', e.target.result);
};
// Function for Deleting Preview Image.
jQuery("#deleteimg").click(function() {
jQuery('#preview').css("display", "none");
jQuery('#file').val("");
});
// Function for Displaying Details of Uploaded Image.
jQuery("#submit").click(function() {
jQuery('#preview').css("display", "none");
jQuery('#message').css("display", "block");
});
});`