1

Can we get uploaded image path in wordpress by filename? Actually I know the way in PHP that will parse all files in the uploads folder, and matches the each file. But that way will produce high load on server, whenever I will run this script.

Is there any other possible way?

Thanks.

Note: I do not have any posts linked with it. I just have image file name. By that only I need to find the full image uploaded path e.g. http://www.abc.com/wp-content/uploads/2013/02/test.png

5
  • You want to find a particular image by its path?
    – s_ha_dum
    Commented Aug 22, 2013 at 13:25
  • yes exactly. I just have image file name, and I want to find it's exact path ion wordpress uploads.
    – Thompson
    Commented Aug 22, 2013 at 15:24
  • See my answer below.
    – s_ha_dum
    Commented Aug 22, 2013 at 15:33
  • how were these images uploaded?
    – Milo
    Commented Aug 22, 2013 at 15:43
  • via custom code, I just add images to server via wordpress function media_sideload_image and using same post ID-1
    – Thompson
    Commented Aug 22, 2013 at 18:13

2 Answers 2

2

I am not 100% sure I understand what you want but...

WordPress treats attached media like a custom post type. The file name, minus the extension, is stored as the post_title in the $wpdb->posts table. So, to get the path just search for the file name minus the extension.

$q = new WP_Query(
  array(
    'fields' => 'ids',
    'name'=>'1011722_472449312832633_575764530_n',
    'post_type' =>'attachment',
    'post_status' => 'inherit',
    'ignore_sticky_posts' => true,
    'posts_per_page' => 1
  )
);
$attid = $q->posts[0];

You can use $attid plus get_attached_file or wp_get_attachment_image or other attachment function to the other information you want.

6
  • sorry dude, that is not my requirement. I only have the image name to find. There is no post ID associated with it.
    – Thompson
    Commented Aug 22, 2013 at 15:34
  • If you have uploaded to the media library this is the way to do it, or at least a way. There is a post ID whether you are aware of it or not
    – s_ha_dum
    Commented Aug 22, 2013 at 17:40
  • I just add images to server via wordpress function media_sideload_image and using same post ID-1
    – Thompson
    Commented Aug 22, 2013 at 18:18
  • Then you may have sidestepped everything that might help you. I'd have to check.
    – s_ha_dum
    Commented Aug 22, 2013 at 18:52
  • You could use either the post title or the slug, but be aware that the field "post_title" in the database must be referred to as "title" and the slug ("post_name" field) as "name" when you use get_posts, so the code above needs to be corrected !!! Commented Nov 25, 2017 at 11:10
1

May be this can help you. I am not writing in detail as I am not sure if it was the actual question.

if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
//selected file to upload
$uploadedfile = $_FILES['attachdoc'];

//wp_handle_upload to upload the file. Check documentation.
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

if ( $movefile && !isset( $movefile['error'] ) ) {
   //uploaded file url
   $attachments = $movefile['url'];
   //uploaded file path
   $attachments = $movefile['file'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.