6

As content author creates a page, she wants to have something like <img src="public://picture.jpg" />. Is it possible? the goal is to avoid hard-coded drupal installation path; that is she doesn't want to have <img src="/drupaltest/sites/all/file/picture.jpg" /> on test site and <img src="/sites/all/file/picture.jpg" /> on production site.

4 Answers 4

3

You should always be using relative paths in content, like <img src="/sites/all/file/picture.jpg" />

That way if you ever change the domain name (for example when you are on the dev environment or the site url changes) you don't have to deal with a lot of headache.

You just need to properly configure your test site to handle the /drupaltest/ part for itself.

The rest of the solutions currently are giving you PHP functions for dealing with file paths, so they won't help you when creating content (PHP really shouldn't be going in your content like that).

To configure to run in a sub-directory

There are a couple of ways you can do that. You could set up your development environment so that apache is pointing to the subdirectory for the web root for that site.

The least technical way though would be to:

  1. In your site's .htaccess file, uncomment the line that says # RewriteBase /drupal and make it say RewriteBase /drupaltest
  2. Install the pathologic module.
  3. Configure the module so that your input formats use pathologic and add your path as /

The pathologic module is useful anyway as it rewrites links in your content to use path aliases instead of paths like node/234.

Be careful when using path auto across multiple domain names, you might find you need to clear the cache whem moving between the testing & live sites to refresh the cached path related content.

4
  • you are spot on - that solutions with PHP functions are not relevant to my question. And also, that my main goal would be solved if I could just have src=/sites/.... The problem, though, that although I have base_url set to .../drupaltest, the path for some reason is evaluated from the root. So, the question that I should have asked is "how do I properly configure my site to handle /drupaltest/ part for itself"? Any pointers?
    – Felix
    Commented Jul 28, 2013 at 18:44
  • Oops sorry, I added some info to the answer.
    – rooby
    Commented Jul 29, 2013 at 4:03
  • I was tinkering with .htaccess - but couldn't make it work. So, I ended up creating a link on test site for sites -> drupaltest/sites. Kludgy - but simple
    – Felix
    Commented Aug 26, 2013 at 5:57
  • I have done that same thing at times. It's a nice simple solution.
    – rooby
    Commented Aug 26, 2013 at 12:25
6

you can use one of this function

file_create_url($uri);

or

image_style_url($style_name, $path)

2

It seems like the question is not about PHP. You can use Token Filter module to make global token values available as an input filter.

<img src="[site:url]sites/default/files/example.jpg"/>

If you install and configure Token Variable module you can type some thing like that.

<img src="[site:url][variables:file_public_path]/example.jpg"/>
5
  • 1
    It is not necessary to use token filter to add the site url to the start of a url. A properly configured site will handle relative links for you without that.
    – rooby
    Commented Jul 29, 2013 at 10:01
  • What kind of configuration you are talking about? If we move site to subdirectory we have to update all img elements in body field.
    – ya.teck
    Commented Jul 29, 2013 at 10:57
  • See my answer above drupal.stackexchange.com/a/80642/10729
    – rooby
    Commented Jul 29, 2013 at 11:03
  • Did you mean using of pathologic module?
    – ya.teck
    Commented Jul 29, 2013 at 11:07
  • 1
    That is one way (easier for the end user than entering a token every time they post a link + it handles aliases). Also if you have more control over your server you can point your domain name direct to the subdir.
    – rooby
    Commented Jul 29, 2013 at 11:55
1

You can also use

theme_image_style($uri)

if you don't want to use an image style.

https://api.drupal.org/api/drupal/modules!image!image.module/function/theme_image_style/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.