Problem in including any file in html while hosting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shreedhan
    New Member
    • Jul 2007
    • 52

    Problem in including any file in html while hosting

    Hi all,

    First of all, I use linux (fedora 6) and apache to host
    I have a problem of including any file in html while hosting only
    Even a simple code like

    [HTML]<img src="/home/user/abc.jpg" />[/HTML]
    won't show the included image.

    To make it more clear,
    If I view same page just opening it (not hosting) like
    file:///home/user/abc.html
    It shows the image included.

    But if I view the webpage using

    Then it won't show any images

    I need to host the page, show how shall I include the image.
    Actually, its not just problem with images only, but every type of file.

    Thanks
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by shreedhan
    Hi all,

    First of all, I use linux (fedora 6) and apache to host
    I have a problem of including any file in html while hosting only
    Even a simple code like

    [HTML]<img src="/home/user/abc.jpg" />[/HTML]
    won't show the included image.

    To make it more clear,
    If I view same page just opening it (not hosting) like
    file:///home/user/abc.html
    It shows the image included.

    But if I view the webpage using

    Then it won't show any images

    I need to host the page, show how shall I include the image.
    Actually, its not just problem with images only, but every type of file.

    Thanks
    Ok, so it works on your local machine but not the hosting machine. Stupid question, but when you uploaded the html page, did you also happen to upload the image(s) as well? If you don't, how will the html page know where to find them?

    You would also have to change the path to wherever you put the images on the hosting server.

    Also, no need to put your localhost URL in your threads. We can't view something that is local to your machine.

    Regards,

    Jeff

    Comment

    • shreedhan
      New Member
      • Jul 2007
      • 52

      #3
      Originally posted by numberwhun
      Ok, so it works on your local machine but not the hosting machine. Stupid question, but when you uploaded the html page, did you also happen to upload the image(s) as well? If you don't, how will the html page know where to find them?

      Thanks for replying. But, yeah, your question was somewhat stupid.
      Let me explain my problem.

      I'm using apache to host (locally) in my own PC (using fedora 6).

      I have a html file which includes an image. (plz remember I'm fully aware that I should give complete path of the image and I have done that)

      Now I can open this file in 2 ways.
      1. Just double click this file to view it in some browser (firefox in my case).
      2. OR put this file into the /var/www/html folder (which is default directory to host a website, as it is htdocs in windows) and view this file as htttp://localhost/filename.html in a browser.

      Case 1 works very fine.
      Case 2 is giving problem. Even I give full path to the image, it isn't showing the image. Only those images which are in same directory or sub-directories are being shown. I can't include anything outside this /var/www/html directory


      I have only one PC and I'm testing in it and not hosting from any other server.
      Help me solve this problem.
      Thanks

      Comment

      • garrow
        New Member
        • Jan 2008
        • 32

        #4
        Your absolute URLS need to reference the web visible path, rather than the filesystem path

        In your example you pointed images to
        Code:
        /home/user/abc.jpg
        On your filesystem these images exist, but the web brower tries to access that location it is looking for
        Code:
        localhost/home/user/abc.jpg
        which the server translates to
        Code:
        /var/www/html/home/user/abc.jpg
        Which doesn't exist.

        So while absolute paths are correct, they must be absolute to the server, not the filesystem.

        I hope this helps.

        You hit the nail on the head with this comment
        I can't include anything outside this /var/www/html directory
        Thats right you cant.
        To do so you would need to make that directory readable by the webserver, and either direct it as a subdir of localhost or create a virtualhost for just that directory.

        The only other option you have is to use a server side language such as PHP and fopen() the file on the server and parse it out.
        The PHP file would still need to be somewhere under the webroot location though.

        Comment

        • garrow
          New Member
          • Jan 2008
          • 32

          #5
          also you dont NEED to use absolute URLs, relative ones are fine.

          Comment

          • shreedhan
            New Member
            • Jul 2007
            • 52

            #6
            Hey,
            Thanks for clearing most of my doubts.
            But stll I have a doubt.


            Originally posted by garrow
            You hit the nail on the head with this comment
            I can't include anything outside this /var/www/html directory
            Thats right you cant.
            To do so you would need to make that directory readable by the webserver, and either direct it as a subdir of localhost or create a virtualhost for just that directory.
            I have created a directory /usr/tmp/uploads
            /usr has read and execute permissions for all
            tmp has read, write and execute permissions for owner, group and others all.
            and I have given read, write, and execute permission to the uploads directory as well.

            Even then it's not showing the image.

            Thanks

            Comment

            • garrow
              New Member
              • Jan 2008
              • 32

              #7
              for security, most webservers are configured not to access files that are outside of the documentroot.
              therefore a web served file CANT access resources that are outside of the document root.

              eg
              root
              /
              documentroot
              /var/www/html/

              so if a file tries to access
              [HTML]href="/"[/HTML]
              it gets
              /var/www/html/
              not
              /

              this means there is no way to access
              /var/
              or /usr/
              unless you configure the webserver documentroot to be something else. ( not recommended.)

              In apache you can create multiple vhosts that have different documentroots, but then they will be different hosts eg
              site1.local and site2.local and would be referenced like
              site1.local/index.html
              etc

              On second thought, the best alternative is to create symlinks to files
              for example

              /var/www/html/lib/
              could be a symlink to
              /usr/lib/myframework/lib/

              This way you could install one copy of your chosen resource, and have it available at both locations.
              also means you could have

              /var/www/html/application1/lib/
              /var/www/html/application2/lib/
              /var/www/html/application3/lib/

              all being symlinks to
              /usr/lib/myframework/lib/

              this way the files are stored outside you webroot, but are available from relative paths WITHIN the webroot.

              Hope that clears that up a bit.

              Comment

              • garrow
                New Member
                • Jan 2008
                • 32

                #8
                In your
                /usr/tmp/uploads
                example.

                2 options.
                You could move that folder into
                /var/www/html/uploads/

                Or
                create a symlink from
                /var/www/html/uploads/
                to
                /usr/tmp/uploads

                That should work.

                I dont know where my brain was last week. :D

                Comment

                • shreedhan
                  New Member
                  • Jul 2007
                  • 52

                  #9
                  Thanks Garrow for the solution.
                  I am now convinced that I can't get outside of /var/www/html (except using symlinks).

                  Comment

                  • garrow
                    New Member
                    • Jan 2008
                    • 32

                    #10
                    wait let me just reiterate that
                    you cant access files outside the webroot :D

                    Comment

                    Working...