-
Notifications
You must be signed in to change notification settings - Fork 9
/
upload.php
36 lines (26 loc) · 1.01 KB
/
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
require_once rtrim( $_SERVER['DOCUMENT_ROOT'], '/' ) . '/wp-load.php';
$current_user = wp_get_current_user();
$role_check_admin = in_array( 'administrator', $current_user->roles );
// Only allow uploads from administrators
if ( $role_check_admin ) {
// Disable PHP warning in order to produce a consistent response.
error_reporting(E_ERROR | E_PARSE);
$response = (object) array();
$upload_dir = wp_upload_dir();
$deploy_path = "{$upload_dir['basedir']}/deploy/";
$deploy_path_url = "{$upload_dir['baseurl']}/deploy/";
// If directory not create, then create it
if ( !file_exists( $deploy_path ) ) {
mkdir( $deploy_path, 0777, true );
}
$upload_file = "$deploy_path{$_FILES['file']['name']}";
$upload_file_url = "$deploy_path_url{$_FILES['file']['name']}";
if ( move_uploaded_file( $_FILES['file']['tmp_name'], $upload_file ) ) {
$response->response = "Success";
$response->url = $upload_file_url;
} else {
$response->response = "Error";
}
echo json_encode( $response );
}