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 3663732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:33:50+00:00 2026-05-19T01:33:50+00:00

This piece of code has previously worked but I have C&P it to a

  • 0

This piece of code has previously worked but I have C&P it to a new place and for some reason, it now won’t work!

        <?
        $user_image = '/images/users/' . $_SESSION['id'] . 'a.jpg';
        if (file_exists(realpath(dirname(__FILE__) . $user_image))) 
        {
            echo '<img src="'.$user_image.'" alt="" />';
        } 
        else 
        {
            echo '<img src="/images/users/small.jpg" alt="" />';
        }
        ?>

As you can see, I am checking for a file, if exists, showing it, if not, showing a placeholder.

The $_SESSION[‘id’] variable does exist and is being used elsewhere within the script.

Any ideas what the problem is?

Thanks

  • 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-05-19T01:33:51+00:00Added an answer on May 19, 2026 at 1:33 am

    Ok lets put it simple:

    You have your images at

    /foo/bar/images/users/*.jpg
    

    and your script was at

    /foo/bar/script.php
    

    before, which worked, because realpath(dirname(__FILE__) . $user_image) creates

    /foo/bar/image/users/*.jpg
    

    But now, when you e.g. moved your script to another directory on the same level (/foo/baz/script.php), the output of the previous command will be

     /foo/baz/image/users/*.jpg
    

    and this path does not exist.

    You said in your comment you moved the script to another directory. If you didn’t move the images too, your script definitely fails.


    Also note there is a difference in accessing the images via a URL (i.e. from the outside) or via a file path (i.e. from the inside). Your images will always be available via www.yourdomain.com/images/users, but if you move your PHP script into another directory, dirname(__FILE__) has to give you another value and thus the test will fail:

    foo/
    |
    - baz/
    | |
    | - script.php <-absolut path: /foo/baz/images/users/...
    |
    - bar/ <- entry point of URL is always here
      |
      - script.php <- absolut path: /foo/bar/images/users/...
      - images/    
        |
        - users/
          |
          - *.jpg
    

    Update:

    If your script is one level below the images, a fix could be:

    file_exists(realpath(dirname(__FILE__) . '/../' . $_SESSION['id'] . 'a.jpg'))
    

    This will generate something like /foo/images/users/v3/../12a.jpg. .. means going up a level.

    Or going up several levels and using $user_image:

    realpath(dirname(__FILE__) . '/../../..' . $user_image)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this piece of code worked fine on my machine, but it doesn't on my
I'm walking in some trouble with this particular piece of code. It has either
I have this piece of code that has a problem. so can you help
The View Programming Guide for iOS on pg. 57 has this piece of code:
This piece of code is supposed to go through a list and preform some
This piece of code used to work in MVC 1 but it does not
I have been struggling to understand some pieces of this code. It asks to
I came across this piece of code today while tutoring some students in a
i was following an ACL tut. which has used this piece of code. class
I have used this piece of code for connecting to the database..It is showing

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.