Easy Way to Hide Image URLs on Your Website

How to Keep Your Image Folder Addresses Private and Serve Images Securely

Posted by Hüseyin Sekmenoğlu on April 07, 2013 Backend Development

One of the hardest things to find online is a clear, working example of how to hide the folder address where your images are stored. I searched everywhere using keywords like "HTML", "img", "url" and "hide" but found nothing complete or straightforward. After some trial and error, I finally figured it out and implemented it on my own site. Here is how you can do it too.


🔍 The Problem

When you insert images on your website using standard HTML, the image source (src) attribute points directly to the image file's location. For example:

<img src="http://www.mywebsite.com/images/photo.jpg" />

This exposes your folder structure and image filenames, which you may want to protect for security, privacy or design reasons.


🔧 The Solution: Serve Images via a PHP Script

Instead of linking images directly, you use a PHP script to serve images dynamically without revealing their real folder path.


🛠️ What You Need

  • A folder filled with your images, for example:
    D:\photos\

  • A PHP file called photo.php to serve images securely

  • Your HTML page where you want to display the images


📂 Step 1: Your HTML Code

To display an image, add the following code in your HTML:

<img src="photo.php?16" />

Here, 16 is the identifier for the image you want to show. The PHP script will use this to find and serve the correct file.


⚙️ Step 2: Create photo.php

Here is the complete PHP script you will need:

<?php
// Get the query string from the URL, e.g., '16'
$address = $_SERVER['QUERY_STRING'];

// If no query string is found, use the default image (0.jpg)
if ($address == "") {
    $address = "D:\\photos\\0.jpg";
} else {
    // Build the path to the requested image file
    $address = "D:\\photos\\" . $address . ".jpg";
}

// Check if the requested image file exists
if (!file_exists($address)) {
    // If it does not exist, use the default image
    $address = "D:\\photos\\0.jpg";
}

// Set the content type header so browser knows it is an image
header("Content-Type: image/jpeg");

// Read and output the image file content
readfile($address);
?>

✅ Step 3: Check Your Setup

  • Make sure the path to your image folder inside the PHP file is correct and matches your server setup.

  • You might need to adjust the backslashes to double backslashes (\\) in PHP strings, as shown.

  • Test by opening photo.php?16 in your browser; it should display the image named 16.jpg from your folder.

  • If it does not work, verify your PHP installation and file permissions.

  • If you have problems, send me a message and I can help troubleshoot.


📝 Summary

This method hides your image folder address by serving images through a PHP script. Your users only see URLs like:

http://www.mywebsite.com/photo.php?16

Instead of direct paths like:

http://www.mywebsite.com/images/16.jpg

This simple trick improves your site security and helps keep your folder structure private without complex server configurations.