XCS
XCS

Reputation: 28137

Select from upper level folder

So, I've created a website and I have lots of pages in the public_html folder. Those page load the images and script from folders who are direct child of public_html folder.

Now I need to move all those files into a folder (let's name it sample-folder).

My question: Is there any way to let the pages inside "sample-folder" access the images and scripts from other folders inside the "public_html" without having to edit all the pages and add a ../images/image.png before each link?

I don't know, some htacces rewrite rule? Some php config.ini edit?

Structure:

public_html
     images
     scripts
     sample-folder
         test.php

Upvotes: 1

Views: 1180

Answers (4)

No Results Found
No Results Found

Reputation: 102745

This .htaccess rule should work (assumes mod_rewrite is enabled):

RewriteRule ^images/(.*)$ sample-folder/images/$1

This would take all requests for files in the (non-existent) images directory and ask for the file in sample-folder/images/ instead.

Hope this is what you were after.

EDIT: My mistake, read the question wrong. Just do this for your paths and the .php files will reference the images correctly no matter how deep in the directory the .php file is:

<img src="/images/myimage.jpg" />

You can use the ../relative/path approach, bit the /leading/forward/slash/approach.jpg should work all the time.

EDIT: If changing the links is not an option for you, you can put this .htaccess file in sample-folder. It will route all requests from within sample-folder for images to /images in the root directory.

RewriteEngine on
RewriteRule ^images/(.*)$ /images/$1

Make sure it goes in sample-folder or whatever you choose to name it.


Keep in mind that this is NOT the best solution. If you start abusing rewrite rules now, your application is going to be a confusing mess very quickly. Typically, I would set a constant, variable, or write a php function that resolves your URLs and/or include paths.

For includes: please see set_include_path()

This should ensure that your php includes get called from the same place no matter where you are in the directory tree. Typically, this would be set in some sort of bootstrap file or any file that is included at the top of all your scripts.

For images, javascript, etc - I highly suggest changing ALL your paths to use the leading forward slash or an absolute URL (preferably in a constant or function call). You're already seeing why writing functions for simple tasks that you do often (like referencing an image in HTML) is a good idea.

It might seem like a pain now to change all these files, but rest assured that this is going to save you loads of headache later. I suggest something similar to this:

  • Download your whole application locally
  • Make a backup
  • Find a text editor that supports find/replace in files
  • Fix all the paths to images, javascript, css, etc. to use the leading forward slash for now
  • Consider utilizing your own php functions from now on, that will save you the effort of ever having to do anything like this again

Upvotes: 1

daalbert
daalbert

Reputation: 1475

You could create a symbolic link from /sample-folder/images/ to /images/. How to create a symbolic link in PHP. Though personally I would just do a Find/Replace to update the links in your source code.

Upvotes: 1

ehudokai
ehudokai

Reputation: 1926

If your images and scripts are in a specific folder that's not changing. Simply make your links to them be absolute instead of relative.

Upvotes: 1

Ross
Ross

Reputation: 46987

If they are in a structure like this (from what it sounds like they are):

/ (document root)
    /images
    /scripts
    /sample-folder
        /test.php

Then you should be able to access an image by ../images/filename.png and /images/filename.png. Referring to the document root is an easier option than managing different levels of directory structure.

Upvotes: 2

Related Questions