Hi i would like to fetch images from name of the image by which i have stored the image from the upload folder. how can i do it in wordpress.

Dani AI

Generated

Brief answer and safe approaches for fetching an uploaded image by filename in WordPress (addresses the hardcoded-path + SQL errors seen in ’s posts and the request from ).

Hard points from the thread: avoid hardcoded relative paths (e.g., "../public_html/..."), don’t rely on raw SQL without preparing/escaping values, and distinguish two cases — the file exists on disk only, or it’s also an attachment in the WordPress Media Library. Prefer WordPress APIs so paths and table prefixes stay correct.

Use the WP API first (attachment if registered, fallback to disk):

<?php
$filename = sanitize_file_name( 'example.jpg' ); // from input
$upload = wp_upload_dir();
$rel = '2015/tc/' . $filename;             // build the subpath as needed
$abs = trailingslashit( $upload['basedir'] ) . $rel;
$url = trailingslashit( $upload['baseurl'] ) . $rel;

$id = attachment_url_to_postid( $url );
if ( $id ) {
  $src = wp_get_attachment_image_src( $id, 'full' );
  echo '<img src="' . esc_url( $src[0] ) . '" alt="" />';
} elseif ( file_exists( $abs ) ) {
  echo '<img src="' . esc_url( $url ) . '" alt="" />';
}

If the image is in the Media Library but attachment_url_to_postid fails, try matching the attachment slug (filename without extension) or a safe DB lookup. Examples:

  • Get by slug: use sanitize_title( pathinfo($filename, PATHINFO_FILENAME) ) + get_posts( ['name'=>$slug,'post_type'=>'attachment'] ).
  • Safe $wpdb query (use $wpdb->prepare and $wpdb->esc_like to avoid SQL errors and wrong prefixes).

Troubleshooting and best practices

  • Confirm wp_upload_dir() values and file permissions; basedir vs baseurl are the correct bases to use.
  • If images are uploaded programmatically, use wp_handle_upload() + wp_insert_attachment() + wp_generate_attachment_metadata().
  • Avoid scanning uploads on every request (glob/scandir each load); cache results with a transient if needed.
  • Always sanitize inputs (sanitize_file_name, esc_url) and use $wpdb->prepare for any direct SQL — most SQL errors come from unescaped values or incorrect table names.

This approach should resolve the path and SQL issues raised by while following the guidance from to stick with reliable, WP-native methods.

Recommended Answers

All 5 Replies

Member Avatar for Member #120589

Show any code that you have so far.

 <?php
//Path to folder which contains images
$dirname = "../public_html/wp-content/uploads/2015/tc/";

//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob($dirname."*");

//Display image using foreach loop
foreach($images as $image) {

//print the image to browser with anchor tag (Use if you want really :) )
echo '<p><a href="'.$image.'" target="_blank"><img src="'.$image.'" alt="filename" /></a></p>';
}
?>
Member Avatar for Member #120589

So this doesn't wwork for you?

ya also i tried other codes but sometimes getting error in sql query or many methods i have tried like uploading image to folder setting up the file path in database then trying to fetch i got stuck with all.
It would be great help if you can provide for the same.

Member Avatar for Member #120589

I.m confused as to what part of the code doesn't work. SQL and writing to db is all down to you, nothing to do with other people's scripts. Your best bet is to stick with a trusted script and persevere IMO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.