Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kalm42
Last active November 26, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalm42/b57d4a4a6788c3586273fe6df8bb27b9 to your computer and use it in GitHub Desktop.
Save kalm42/b57d4a4a6788c3586273fe6df8bb27b9 to your computer and use it in GitHub Desktop.
WordPress File Upload
"media_details": {
"width": 4000,
"height": 2668,
"file": "2018/11/IMG_2344.jpg",
"sizes": {
"thumbnail": {
"file": "IMG_2344-150x150.jpg",
"width": 150,
"height": 150,
"mime_type": "image/jpeg",
"source_url": "http://0.0.0.0/wp/wp-content/uploads/2018/11/"
},
"medium": {
"file": "IMG_2344-300x200.jpg",
"width": 300,
"height": 200,
"mime_type": "image/jpeg",
"source_url": "http://0.0.0.0/wp/wp-content/uploads/2018/11/"
},
"medium_large": {
"file": "IMG_2344-768x512.jpg",
"width": 768,
"height": 512,
"mime_type": "image/jpeg",
"source_url": "http://0.0.0.0/wp/wp-content/uploads/2018/11/"
},
"large": {
"file": "IMG_2344-1024x683.jpg",
"width": 1024,
"height": 683,
"mime_type": "image/jpeg",
"source_url": "http://0.0.0.0/wp/wp-content/uploads/2018/11/"
},
"full": {
"file": "11",
"width": 4000,
"height": 2668,
"mime_type": "image/jpeg",
"source_url": "http://0.0.0.0/wp/wp-content/uploads/2018/11/"
}
},
function handle_image_upload() {
$errors = [];
$file = [];
// Make sure there is a file to handle
if (isset( $_FILES['files'] )) {
// Allowed extensions
$extensions = ['png', 'jpg', 'jpeg', 'gif'];
// Directorys and urls to upload to
$wp_upload_dir = wp_upload_dir();
// Gather required variables
$file_name = wp_unslash($_FILES['files']['name']);
$filetype = wp_check_filetype( basename( $file_name ), null );
$tmp_file = wp_unslash($_FILES['files']['tmp_name']);
// Copy the file from the tmp directory to the uploads directory.
$unique_file_name = wp_unique_filename( $wp_upload_dir['path'], basename( $file_name ) );
$new_file = $wp_upload_dir['path'] . '/' . $file_name;
// if (false === @copy( $tmp_file, $new_file )) {
// error_log("Failed to copy " . $new_file);
// return new WP_Error('upload_error', sprintf( __( 'The selected file could not be copied to %s.', 'add-from-server' ), $wp_upload_dir['path'] ));
// }
// ensure valid file type
for ($i=0; $i < count($extensions); $i++) {
if (false === strpos($filetype['type'], $extensions[$i])) {
return new WP_Error('upload_error', sprintf( __( 'The selected file could not be copied to %s because it is not an allowed file type.', 'add-from-server' ), $wp_upload_dir['path'] ));
}
}
// Move the file from the temporary directory to the target upload path.
$target_path = $wp_upload_dir['path']. "/" . $unique_file_name;
// $moved_file = move_uploaded_file( $tmp_file, $target_path );
if (false === move_uploaded_file( $tmp_file, $target_path )) {
error_log("Failed to copy " . $new_file);
return new WP_Error('upload_error', sprintf( __( 'The selected file could not be copied to %s.', 'add-from-server' ), $wp_upload_dir['path'] ));
}
// Compute the URL
$url = $wp_upload_dir['url'] . '/' . $file_name;
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $url,
'post_mime_type' => $filetype['type'],
'post_title' => sanitize_file_name($file_name),
'post_content' => '',
'post_status' => 'inherit',
'post_author' => 1,
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $target_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
$file['id'] = $attach_id;
}
return $file;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment