im new in web development and i wanna know, how can make a dynamic file path or dynamic source directory. its like :

<img id="branding-logo" src="/image/logo.png">

<link rel="stylesheet" href="/images/green.css">

<script src="/js/jquery.js"></script>

main {
background: url('/images/CAT119/hp-images/hptilebggreen.jpg') top left repeat-x;
}

so basically what i want to know is how can i do that on my project.. that forward slash on the very begining og the path.

Recommended Answers

All 4 Replies

Member Avatar for diafol
<?php
    $img_loc = '/whatever';
?>
...

<img id="branding-logo" src="<?php echo $img_loc;?>/logo.png"><img id="branding-logo" src="/image/logo.png">

You could do something like that. usually we set up config files stored above the public root and these contain things like DB connection details and site constants. It could be a useful place to store things like this too.

From your question above it's quite hard to identify what it is you're actually asking for, so I'll cover file paths in general.

1) Relative File Paths

A relative file path looking like this: <a href="link.php">Link</a> and this is because you can't see the full web address. The name 'relative' is pretty fitting as the link to the file relates to where the current file is located. Examples of relative file paths:

  1. /images/image.jpg
  2. index.php
  3. /folder1/folder2/folder3/file.php

2) Absolute File Paths

An absolute file path can look like any of the following:

  1. /home/www/public/website.com/public/css/theme1/style.css
  2. http://www.example.com/images/banners/banner1.png
  3. http://000.000.000.000/js/jquery.js

It's called absolute because it will work no matter where the current file is located because it starts looking for the referenced file from root level.

I usually always use absolute web addresses, therefore nothing breaks if I ever need to shuffle or reorganise my files and folders (as long as my assets are kept seperate so their location never changes, of course).

My web apps and sites have this typical structure:

/home/www-data/public/website1.com/public/assets/scripts
/home/www-data/public/website1.com/public/assets/css
/home/www-data/public/website1.com/public/assets/images
/home/www-data/public/website1.com/public/

My assets, such as images, classes, stylesheets and scripts are stored seperately from my applications/sites files, which start diretly in public. I can shuffle them about and do whatever I like with them as all of my file dependencies and assets are called with the absolute path of:

/home/www-data/public/website1.com/public/assets/[file_i_want_here]

I usually simplify it further by creating a variable in my config file:

$ASSETS = "/home/www-data/public/website1.com/public/assets/";

and then I can include a style.css anywhere using this:

echo $ASSETS."css/style.css";

or an image using this:

echo $ASSETS."images/logo.gif";

I'm sure there are better ways, like using constants, but I like this :)

Member Avatar for diafol

Absolute paths work well until you change hosts :)

But that is easily fixed if you have something like:

$public_url = '/home/www-data/public/website1.com/public/';
$images_url = $public_url . 'images';

Then you just need to fix the $public_url once in one file. ALso look up __DIR__

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.