Question

I store a lot of HTML contents in DB
That i have print in many dynamic pages
For example

// returned from DB
$contentHTML = 
'
    <h1>Photo Number 1</h1>
    <p>description1</p>
    <p><img src="../images/1.jpg"/></p>
    <h1>Photo Number 2</h1>
    <p>description2</p>
    <p><img src="../images/2.jpg"/></p>
';
// Print HTML content
echo $contentHTML;

My problem is that
My HTML code contain images URLs
And my current images folder (images) in application server

But in the future its expected to change this folder

  • To be in another folder
  • To be in another server

So it will be a very complex and hard task to replace all images URLs

Then
Any idea or trick for
How to embed dynamic URLs of Images in HTML content stored in DB
Or how make it configurable ?

Was it helpful?

Solution

I usually make an ad-hoc script for this kind of scenarios.

Step 1:

Replace all hard links with dynamic known variable.

../images/ to [[PUBLIC_PATH]][[IMG_DIR]]

Step 2:

Before use, replace the actual server address and image dir with variable you have added.

E.g.

contentHTML  = NDatabase::getAssoc("select myField from myTable");//yada yada      
$contentHTML = 
'
    <h1>Photo Number 1</h1>
    <p>description1</p>
    <p><img src="[[PUBLIC_PATH]][[IMG_DIR]]/1.jpg"/></p>
    <h1>Photo Number 2</h1>
    <p>description2</p>
    <p><img src="[[PUBLIC_PATH]][[IMG_DIR]]/2.jpg"/></p>
';

$myNewServerPath = "http://www.mysite.come/en/";
$myPublicFolder = "{myNewServerPath}public/";
$myImgDir = "img"



 $newContent = str_replace(array("[[PUBLIC_PATH]]","[[IMG_DIR]]") ,  array($myPublicFolder,$myImgDir )  ,$contentHTML  )

After that you can either update your DB or just use the code as it's.

Personally, I have a file full of list of constants dynamically generated to to get the site name, site URL, site sub folders etc.

define("CURR_PATH","http://$_SERVER[SERVER_NAME]/");//

Guess you got the idea.

OTHER TIPS

you can use mod_rewrite to rewrite URLs to point to the new location.

eg:

RewriteRule ^images/(.*)$ new-images-folder/$1 [NC,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top