Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:14:02+00:00 2026-06-11T09:14:02+00:00

I have been having a hard time with what must be an incredibly normal

  • 0

I have been having a hard time with what must be an incredibly normal task. I upload and save images to my web server and save the path to the file in MySQL data base (this is all working). The thing that doesn’t work is fetching an image file from the server and displaying it on the page via ajax.

Originally I was trying to just retrieve the path from the database, and update an tag’s src attribute with the path to the image. This was working, but this way all the images are in a folder on the server where people all have access to them. This is not good. I can only have the pictures that belong to certain users accessible by these users.

In order to restrict access to these photos I added an Apache directive on that folder, which successfully restricted access. The problem then became that I could not display the images in the browser by setting the src attribute to that path. See my post: https://serverfault.com/questions/425974/apache-deny-access-to-images-folder-but-still-able-to-display-via-img-on-site

Finally I have learned that I have to use PHP to read the image data directly from the server and send this data to the browser. I have used the file_get_contents() function, which works to convert the image file (PNG) on the server into a string. I return this string to the browser in an ajax call. The thing I can’t get is: how to convert this string back into an image using JavaScript?

See this code:

$.ajax({
    url: get_image.php,
    success: function(image_string){
        //how to load this image string from file_get_contents to the browser??
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:14:03+00:00Added an answer on June 11, 2026 at 9:14 am

    You could display a default “no access” image to users who are forbidden to access the image:

    <?php
    
    $file = $_GET['file']; // don't forget to sanitize this!
    
    header('Content-type: image/png');
    if (user_is_allowed_to_access($file)) {
        readfile($file);
    }
    else {
        readfile('some/default/file.png');
    }
    

    And, on the client side:

    <img src="script.php?file=path/to/file.png" />
    

    Alternatively, if you really really want or need to send the data via Ajax, you can Base64 encode it:

    <?php
    
    echo base64_encode(file_get_contents($file));
    

    And place the response in an img tag using the Data URI scheme

    var img = '<img src="data:image/png;base64,'+ server_reponse +'"/>';
    

    Given that the Base64 encoded data is significantly larger than the original, you could send the raw data and encode it in the browser using a library.


    Does that make sense to you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having a hard time understanding how to use a singleton to
So I have been having a hard time sending email from my school's email
I have been having a really hard time debugging this error since the past
I am having a hard time with my image upload script, getting the images
I have been having hard time with Primefaces and SO is my ultimate place
I have been having a hard time googling an answer for this, but.... can
I have been looking at self hosting NuGet, having a hard time understanding how
i have been having a hard time to find anything that is usefull but
Ok, I have been having a VERY hard time figuring this out. I guess
This is probably a basic question, but I have been having a hard time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.