domjanzsoo
domjanzsoo

Reputation: 141

wordpress wp_handle_upload not moving the file

Can someone tell me why this function is not moving the uploaded files anywhere.

function handle_logo_upload($option){
    if(!function_exists('wp_handle_upload'))
{

            require_once(ABSPATH .'wp-admin/includes/file.php');
                                    }


            if(!empty($_FILES["site_logo_custom"])){

$theFile=$_FILES["site_logo_custom"];

$overrides=array('test_form'=>false);

$urls=wp_handle_upload($theFile,$overrides);
$temp=$urls["url"];
return $temp;

                                    }


                                    return $option;
                                }

I can't really find too much about wp_handle_upload function. Thankss!!!

Upvotes: 0

Views: 7451

Answers (2)

Sajjad Hossain Sagor
Sajjad Hossain Sagor

Reputation: 518

I assume your form is formatted kinda like this :

form action="" enctype="multipart/form-data" method="post"> //action is current post
 <input type="file" name="file">
 <input type="submit" name="submit">
</form>

And to upload the file to the wordpress upload folder using wp_handle_upload(); function you may use below code....

function handle_logo_upload($file){

require_once(ABSPATH.'wp-admin/includes/file.php');
$uploadedfile = $file;

$movefile = wp_handle_upload($uploadedfile, array('test_form' => false)); 

if ( $movefile ){
    echo $movefile['url'];
    //or return
    return $movefile['url'];
  }

}
if (isset($_POST['submit'])) {
   handle_logo_upload($_FILES['file']);
}

Upvotes: 2

Akshay Shah
Akshay Shah

Reputation: 3504

Guessing that your function is called perfectly.

function handle_logo_upload($option){
    if(!function_exists('wp_handle_upload'))
    {

        require_once(ABSPATH .'wp-admin/includes/file.php');
    }
    //you are using empty version make sure your php version is higher than 5.2
    if(!empty($_FILES["site_logo_custom"])){
        $move_logo = wp_handle_upload( $_FILES["site_logo_custom"], array('test_form' => false) );
        if ( $move_logo && !isset($move_logo['error']) ) {
            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                'guid' => $wp_upload_dir['url'] . '/' . basename($move_logo['file']),
                'post_mime_type' => $move_logo['type'],
                'post_title' => preg_replace( '/\.[^.]+$/', '', basename($move_logo['file']) ),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            $logo_attach_id = wp_insert_attachment($attachment, $move_logo['file']);
            $image_attributes = wp_get_attachment_image_src( $logo_attach_id );
            if ( $image_attributes ) {
              return $image_attributes[0]; 
             }
             else
             {
                return $option;
             }
        }else{
            return $option;
        }
    }else{
        return $option;
    }

}          

Please read my comments written in code

Upvotes: 1

Related Questions