• Hi there,

    I’m using wp_insert_attachment in another PHP file to upload images, instead of using WP-Admin to upload.

    – When I use the ‘WP-Admin > Media > Upload Image’ to upload and create attachments, it creates the attachment URL as it should:
    http(s)://mybucket.myregion.amazonaws.com/objectpath/filename.jpg

    My problem is that: When I upload images with another PHP file (uploadimage.php for example) using the ‘wp_insert_attachment’ function, it doesn’t upload to S3, and it creates the URL as this:
    http(s)://mybucket.myregion.amazonaws.com/objectpath leaving out the /filename.jpg part of the URL?

    Here is my PHP file using the ‘wp_insert_attachment’ function:

    <?php //Collect Base64 and convert to file
    
    $parent_post_id = $_POST['newitemid'];
    
    $upload_dir       = wp_upload_dir();
    
    // @new
    $upload_path      = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
    
    $decoded          = base64_decode( $_POST['base64File']) ;
    
    $filename         = 'newImage.jpg';
    
    $hashed_filename  = md5( $filename . microtime() ) . '_' . $filename;
    
    // @new
    $image_upload     = file_put_contents( $upload_path . $hashed_filename, $decoded );
    
    //HANDLE UPLOADED FILE
    if( !function_exists( 'wp_handle_sideload' ) ) {
    
      require_once( ABSPATH . 'wp-admin/includes/file.php' );
    
    }
    
    // Without that I'm getting a debug error!?
    if( !function_exists( 'wp_get_current_user' ) ) {
    
      require_once( ABSPATH . 'wp-includes/pluggable.php' );
    
    }
    
    // @new
    $file             = array();
    $file['error']    = '';
    $file['tmp_name'] = $upload_path . $hashed_filename;
    $file['name']     = $hashed_filename;
    $file['type']     = 'image/jpg';
    $file['size']     = filesize( $upload_path . $hashed_filename );
    
    // upload file to server
    // @new use $file instead of $image_upload
    $file_return      = wp_handle_sideload( $file, array( 'test_form' => false ) );
    
        //return error if there is one
        if(isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
         $jsonReturn = array(
          'error'  =>  'Error uploading.',
          'code' => '95234'
          );
       } else {
       /**
        * See http://codex.wordpress.org/Function_Reference/wp_insert_attachment
        */
       $filename = $file_return['file'];
       $attachment = array(
         'post_mime_type' => $file_return['type'],
         'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
         'post_content' => '',
         'post_status' => 'inherit',
         'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
         );
       $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
       // you must first include the image.php file
       // for the function wp_generate_attachment_metadata() to work
       require_once(ABSPATH . 'wp-admin/includes/image.php');
       $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
       wp_update_attachment_metadata( $attach_id, $attach_data );
    
    	$id = $attach_id;
    
    //	$urlpart1 = wp_upload_dir();
    	$imgurl = wp_get_attachment_url( $id );
    
    	$stripurl = stripslashes($imgurl);
    
        $arrjson = array ('imageid'=>$attach_id,'imageurl'=>$stripurl);
    
        echo json_encode($arrjson);
     }
    
    ?>

    It creates the attachment without any problems; but for the past 2 days, I’ve been trying to figure out why it doesn’t upload to Amazon S3 when the script is run, and why it annoyingly leaves out the /filename.jpg part of the attachment URL?

    I know it seems a bit off-topic, but if anyone could help me, I would really appreciate it! Thanks

    https://wordpress.org/plugins/amazon-s3-and-cloudfront/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Iain Poulson

    (@polevaultweb)

    It looks like you are using $wp_upload_dir instead of $upload_dir when building the $attachment arrray –

    $attachment = array(
    ‘post_mime_type’ => $file_return[‘type’],
    ‘post_title’ => preg_replace( ‘/\.[^.]+$/’, ”, basename( $filename ) ),
    ‘post_content’ => ”,
    ‘post_status’ => ‘inherit’,
    ‘guid’ => $upload_dir[‘url’] . ‘/’ . basename( $filename )
    );

    After fixing that the code worked for me

    Thread Starter pixeledmedia

    (@pixeledmedia)

    Hi thanks for the reply,

    Unfortunately, this did not work for me; it still uploads an image to the server without problems, but its like it gets ‘stuck’ or stops exactly when it needs to copy the images over to S3.

    It still creates the attachment URL, leaving out the filename as: http(s)://myregion.mybucket.amazonaws.com/objectpath

    instead of the full URL, with the filename:
    http(s)://myregion.mybucket.amazonaws.com/objectpath/filename.jpg.

    Is it possible that I can see the full code you used to make it work? Thanks alot!

    Plugin Contributor Iain Poulson

    (@polevaultweb)

    I was using this in a separate plugin – https://gist.github.com/polevaultweb/b5691909f2b35fd2b121

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Attachment URLs problem’ is closed to new replies.