Copy image to my server direct from URL and upload it to WordPress uploads folder?
我正在尝试通过PHP脚本上传图像,如下所述。 我正在从URL拍摄图像;
我已经从这篇文章中获取了参考资料实际上,就我而言,我想将其上传到WordPress上传文件夹中,其中上传了发布图片,并且您知道WordPress在运行时在'wp-content / uploads /'内创建了一个文件夹,并将其上传到那里。 因此,路径($ save_path)不是在运行时决定的。
1 2 3 4 | $url="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png"; $contents=file_get_contents($url); $save_path="/path/to/the/dir/and/image.jpg"; file_put_contents($save_path,$contents); |
我正在尝试使用WordPress函数" media_handle_upload"上传图像,而不是" file_put_contents",但是一旦获得文件内容(
1 | $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] ); |
100%使用此代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | include_once( ABSPATH . 'wp-admin/includes/image.php' ); $imageurl = '<IMAGE URL>'; $imagetype = end(explode('/', getimagesize($imageurl)['mime'])); $uniq_name = date('dmY').''.(int) microtime(true); $filename = $uniq_name.'.'.$imagetype; $uploaddir = wp_upload_dir(); $uploadfile = $uploaddir['path'] . '/' . $filename; $contents= file_get_contents($imageurl); $savefile = fopen($uploadfile, 'w'); fwrite($savefile, $contents); fclose($savefile); $wp_filetype = wp_check_filetype(basename($filename), null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => $filename, 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $uploadfile ); $imagenew = get_post( $attach_id ); $fullsizepath = get_attached_file( $imagenew->ID ); $attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath ); wp_update_attachment_metadata( $attach_id, $attach_data ); echo $attach_id; |
这是完整的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // $filename should be the path to a file in the upload directory. $filename = '/path/to/uploads/2013/03/filename.jpg'; // The ID of the post this attachment is for. $parent_post_id = 37; // Check the type of file. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype( basename( $filename ), null ); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); // Prepare an array of post data for the attachment. $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\\.[^.]+$/', '', basename( $filename ) ), 'post_content' => '', 'post_status' => 'inherit' ); // Insert the attachment. $attach_id = wp_insert_attachment( $attachment, $filename, $parent_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( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $parent_post_id, $attach_id ); |
您可以检查wp_insert_attachment()了解更多信息