• EDIT: If anyone find this function useful – feel free to use it. I was originally created it with a friend in order to get a file from Gravity Forms file upload field and use it as a featured image for a given post.

    I have this cutie:

    // Insert picture to Media Library
    
    function set_featured_image( $post_id, $file_path_old, $end_folder = '', $filename = '' ) {
    
    	// $file_path_old is actually URL so first convert it to path
    
    	$file_path_old = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $file_path_old );
    
    	// Check the type of file
    
    	$file_type_and_ext = wp_check_filetype( basename( $file_path_old ), null );
    
    	// If no filename was supplied, rename the file according post title
    
    	if ( empty( $filename ) )
    	{
    		$filename = basename( get_permalink( $post_id ) ) . '.' . $file_type_and_ext['ext'];
    	}
    	else
    	{
    		$filename .= $file_type_and_ext['ext'];
    	}
    
    	// Prepare category subfolder
    
    	$categories = get_the_category( $post_id );
    
    	// Prepare the new path and filename
    
    	if ( !empty ( $end_folder ) )
    	{
    		$file_path_new = WP_CONTENT_DIR . '/uploads/' . $categories[0]->slug . '/' . $end_folder . '/' . $filename;
    	}
    	else
    	{
    		$file_path_new = WP_CONTENT_DIR . '/uploads/' . $categories[0]->slug . '/' . $filename;
    	}
    
    	// Move the file from the old path to the new one and rename it
    
    	rename( $file_path_old, $file_path_new );
    
    	// Prepare an array of post data for the attachment
    
    	$file_url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $file_path_new );
    
    	$attachment = array(
    		'guid'				=> $file_url,
    		'post_mime_type'	=> $file_type_and_ext['type'],
    		'post_title'		=> preg_replace( '/\.[^.]+$/', '', $filename ),
    		'post_content'		=> '',
    		'post_status'		=> 'inherit'
    	);
    
    	// Insert the attachment
    
    	$attachment_id = wp_insert_attachment( $attachment, $file_path_new, $post_id );
    
    	// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it
    
    	require_once( ABSPATH . 'wp-admin/includes/image.php' );
    
    	// Generate the metadata for the attachment, and update the database record
    
    	$attach_data = wp_generate_attachment_metadata( $attachment_id, $file_url );
    
    	wp_update_attachment_metadata( $attachment_id, $attach_data );
    
    	set_post_thumbnail( $post_id, $attachment_id);
    }

    And I think I get the errors because of permission issues, as I don’t use the default wp_upload_dir(). Here are the errors:

    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-150x150.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-300x300.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-400x272.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-320x202.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-52x50.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-180x138.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-400x272.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-400x295.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-300x214.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-220x161.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396
    [27-May-2014 13:42:52 UTC] PHP Warning:  fopen(https://test.servix.co.il/wp-content/test-folder/sample-66x66.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/servixcare/public_html/test/wp-includes/class-wp-image-editor.php on line 396

    So if anyone can help me debug this it would be sooo awesome, and also if you have any other recommendations about the code – please tell me ??

    Thanks!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The problem is specifying an URL to fopen() for write mode, you need to provide a valid path instead. I’m not sure where this URL is coming from, the str_replace() function appears correct. If it is coming from wp_insert_attachment() in some way, then feeding that function an URL apparently does not work as well as you previously claimed ??

    I’m just having a bit of fun, there’s nothing to indicate this is related to inserting attachments with an URL, and you did say you would fix that issue anyway.

    Thread Starter rechazame

    (@rechazame)

    bcworkz – YOU DA MAN.

    Turned out I was passing a URL instead of a PATH, to wp_generate_attachment_metadata(), which generated these errors.

    Thank you for your help bro

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Error in from fopen() in my brand new set_featured_image() func’ is closed to new replies.