wp 서버에 다운로드 된 파일을 미디어로 저장 > IT 기술백서

IT 기술백서

직접 알아내거나 검색하기 귀찮아서 모아 둔 것

php | wp 서버에 다운로드 된 파일을 미디어로 저장

본문

[code]

function downloadImage($url)

{

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

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

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


    $tmp = download_url($imageurl);


    $ext = image_type_to_extension(exif_imagetype($tmp));


    if (empty($ext)) {

        return;

    }


    if (is_wp_error($tmp)) {

        @unlink($file_array['tmp_name']);

        return $tmp;

    }


    $name = basename($imageurl);


    $tmpnew = $tmp.'.'.$ext;

    if (!rename($tmp, $tmpnew)) return;

    

    $name = pathinfo($imageurl, PATHINFO_FILENAME).$ext;

    

    $file_array = array(

        'name' => $name,

        'tmp_name' => $tmpnew

    );


    $id = media_handle_sideload($file_array, 0);


    if (is_wp_error($id)) {

        @unlink($file_array['tmp_name']);

        return $id;

    }


    $attach_url = wp_get_attachment_url($id);


    return $attach_url;

}

[/code]

댓글 0개

등록된 댓글이 없습니다.

Menu