Check if attachment files already exists before fetching them remotely.
-
I’d like to propose a small addition to fetch_remote_file() that allows a bypass so it will be possible to check for local existence of files instead of importing them via a remote GET request.
Basically replace
$headers = wp_get_http( $url, $upload['file'] );
with
// fetch the remote url and write it to the placeholder // Sometimes you already have all the files in place, so instead // of downloading them again better see if you can reuse them // put // define( 'WP_IMPORTER_CHECK_LOCAL_FILES', true ); // in you wp-config.php to do so. $existing_upload = wp_upload_dir( $post['upload_date'] ); $existing_upload['file'] = $existing_upload['path'] . "/$file_name"; $existing_upload['url'] = $existing_upload['url'] . "/$file_name"; if ( defined( 'WP_IMPORTER_CHECK_LOCAL_FILES' ) && true === WP_IMPORTER_CHECK_LOCAL_FILES && file_exists( $existing_upload['file'] ) && filesize( $existing_upload['file'] ) > 0 ) { $headers = array( 'response' => 200, 'content-length' => filesize( $existing_upload['file'] ), 'x-final-location' => $url ); $upload = $existing_upload; } else { $headers = wp_get_http( $url, $upload['file'] ); }
Then define
define( 'WP_IMPORTER_CHECK_LOCAL_FILES', true );
in your wp-config.php and existing files would be kept rather than re-fetched.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Check if attachment files already exists before fetching them remotely.’ is closed to new replies.