Get uploaded image path in wordpress by filename

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?

Read More

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

Related posts

2 comments

  1. 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.

  2. 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'];
    }
    

Comments are closed.