Wrong URL when using wp_insert_attachment

I created a plugin as 3rd party to acomodate system with wordpress website on main site. So the scenario is:
when user hit submit on the system it will also added to wordpress website, it’s working perfect, no problem at all. but when i try to set the featured image through wp_insert_attachment it keep me give a URL like

http://xxxxx.com/wp-content/uploads/http://xxxxx.com/system/media/.../xx.jpg

what i want to be is only http://xxxxx.com/system/media/…/xx.jpg saved as featured image, is it possible to do so?
here is my current script

Read More
if($pt == "pictures"){
        $filename_url = $_GET["dml_file"];
        $mime = wp_check_filetype($filename_url, null);
        $data = array(
         'post_mime_type' => $mime['type'],
         'post_title' => preg_replace('/.[^.]+$/', '', basename($_GET["dml_file"])),
         'post_content' => '',
         'post_status' => 'inherit'
        );
        $attachment_id = wp_insert_attachment($data, $filename_url, $pid);
        update_post_meta($pid, $custom_field, $attachment_id);
      }else{
        update_post_meta($pid, $custom_field, $_GET["dml_file"]);
      }

I have tried to use file_get_contents and file_put_contents to create image in WP installation, but I don’t want that way.

The system is submitting this:

http://user:pass!@localhost/wp-content/plugins/dml3rdparty/dmlsubmit.php?dml_sa‌ve=save&dml_file=http://xxx.xxx.xxx.xxx/dml/assets/media/Accreditation/download.d‌15bdf4e9e.jpg&dml_type=Print Quality Photos&dml_description=test|download.jpg&dml_status=publish

Related posts

Leave a Reply

2 comments

  1. This function gets the file content using cURL:

    function my_file_get_contents($url){
        $options = array(
            CURLOPT_AUTOREFERER => true,
            CURLOPT_HEADER => false,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_CONNECTTIMEOUT => 120,
            CURLOPT_TIMEOUT => 120,
            CURLOPT_MAXREDIRS => 10
        );
        $ch = curl_init($url);
        curl_setopt_array($ch,$options);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    

    This function uses the above to get the image, saves it to the uploads folder, and sets it as a featured image for a post:

    function my_featured_image($image_url,$post_id){
        $upload_dir = wp_upload_dir();
        $image_data = my_file_get_contents($image_url);
        $filename = strtok($image_url, '?');
        $filename = basename($filename);
        if(wp_mkdir_p($upload_dir["path"])){
            $file = $upload_dir["path"]."/".$filename;
        }else{
            $file = $upload_dir["basedir"]."/".$filename;
        }
        file_put_contents($file, $image_data);
        $wp_filetype = wp_check_filetype($filename,null);
        $post_author = get_post_field("post_author",$post_id);
        $attachment = array(
            "post_author" => $post_author,
            "post_mime_type" => $wp_filetype["type"],
            "post_title" => sanitize_file_name($filename),
            "post_content" => "",
            "post_status" => "inherit"
        );
        $attach_id = wp_insert_attachment($attachment,$file,$post_id);
        require_once(ABSPATH."wp-admin/includes/image.php");
        $attach_data = wp_generate_attachment_metadata($attach_id,$file);
        $res1 = wp_update_attachment_metadata($attach_id,$attach_data);
        $res2 = set_post_thumbnail($post_id, $attach_id);
    }
    

    Call with:

    my_featured_image($image_url,$post_id);