php - How to store custom field uploaded photo into database in wordpress


I have a custom field(file) in my pagetemplate.

<input type="file" name="head-photo" size="40" class="wpcf7-form-control form-control" id="uploadImage" aria-invalid="false" />

How can i store uploaded file(photo) in to my wordpress database?




if you use this field to save any post detail than, you can save this field value as add_post_meta($post_id, $meta_key, $meta_value, $unique); for update update_post_meta($post_id, $meta_key, $meta_value, $prev_value);

Please check




Assuming you are using a simple form with input file you can use media_handle_upload function,

Example handler on form submit

// Load necessary files
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

// Simple image attachment data             
$atts_data = array(
    'post_author'   => get_current_user_id(), // just an example

// Process image upload
$attachment_id = media_handle_upload( 'input-file-name', 0, $atts_data );

// verify upload 
if ( !is_wp_error( $attachment_id ) ) { 
    // Media upload success, 
} else {
    // Media Upload Fail

People are also looking for solutions to the problem: recursion - Issue recursively deleting subdirectories php


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.