i'm planning to put an image before the name of the file. how to do that? for example i have IMAGE1.jpg filename i want to put an JPG image before the filename.

Dani AI

Generated

Quick summary and robust alternative to the extension-icon idea already suggested by (and the clarifying prompt from ): mapping file types to small icons works fine for a simple listing, but for a reliable, secure solution use server-side detection, proper filename sanitization, and a sensible fallback. Below are practical steps and a compact example to adapt.

Use server-side detection instead of trusting only the name. Get the extension with pathinfo() and normalize to lowercase. Where possible verify the actual MIME type (for images use finfo_file() or exif_imagetype()) so an uploaded file with a forged extension does not get the wrong icon. Keep an associative icon map and a generic fallback for unknown types. Always HTML-escape output to avoid XSS when showing filenames or icon paths.

Example pattern (concise):

// determine extension and server-detected MIME, then pick an icon key
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filePath);
finfo_close($finfo);

$iconKey = preg_match('#^image/#', $mime) ? 'image' : $ext;
$icons = ['image' => 'icons/image-generic.svg', 'pdf' => 'icons/pdf.svg'];
$icon = isset($icons[$iconKey]) ? $icons[$iconKey] : $icons['generic'];

echo '<img src="'.htmlspecialchars($icon, ENT_QUOTES).'" alt="'.htmlspecialchars(strtoupper($iconKey).' file', ENT_QUOTES).'" /> '
    .htmlspecialchars(basename($filePath), ENT_QUOTES);

Notes and cautions:

  • Handle multi-dot names (archive.tar.gz) explicitly if you need composite extensions.
  • Prebuild the icon map and use a sprite or inline SVGs to reduce requests.
  • Protect the icons directory (not writable by uploads) and do server-side validation for uploads; see the PHP docs for pathinfo() and finfo_file() and OWASP guidance on secure file uploads for production hardening:
    PHP pathinfo()
    PHP finfo_file()
    OWASP File Upload Cheat Sheet

For quick demos, 's mapping is perfectly serviceable. For anything user-facing or public, apply the checks above.

Recommended Answers

All 4 Replies

I think your going to have to better explain what you are trying to do. You have IMAGE1.jpg and you want to make that JPGIMAGE1.jpg?

example like this. i found a good example, try to click the advance editor here in daniweb. then click manage attachment in below of that you can see

<image of the psd>psd files

You have to collect all image type extensions icon in one image folder.and rename it as given examples.
e.g.
jpg => jpg_type.png
png => png_type.png
gif => gif_type.png

Store all above image in one folder called 'image'.
Then try below code.

<?
	$imageFile = "imagename.jpg";
	$extAr = explode('.',$imageFile);
	$ext = strtolower($extAr[count($extAr)-1]);
	$iconPath = 'image/'.$ext.'_type.png'; // jpg_type.png
	echo '<img src="'.$iconPath.'" />'.$imageFile;	
?>

thank you vibhadevit :)

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.