Jump to content

PHP Newb: How can I get absolute path for images?


xstortionist

Recommended Posts

I'm working on an email template system where my client will go to a page and copy & paste the source code into their crm system.

 

I'm trying to find a way to automatically replace the image path without the use of echo if possible.

 

I've figured this out already with this script:

 

<?
function GetFileDir($php_self){
$filename = explode("/", $php_self); // THIS WILL BREAK DOWN THE PATH INTO AN ARRAY
for( $i = 0; $i < (count($filename) - 1); ++$i ) {
$filename2 .= $filename[$i].'/';
}
return $filename2;
}
?>

 

But I don't wanna have to go in and put

<?php echo GetFileDir("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>

before all the image paths.

 

So basically anywhere that src="images/whatever.jpg" is listed it will replace "images/" with "http://www.mydomain.com/images/whatever.jpg"

 

is it possible to do this without having to place code before "images" ?

Link to comment
Share on other sites

But why? In html, src="/images/pic.png" produces the same result as src="http://www.mydomain.com/images/pic.png" as long as the image resource resides in the same domain as the html page that's loading it.

 

The reason why I need this to happen is because you can't copy source code from a page and send it in an email blasts. The images will not show because the URL isn't absolute for the image source.

Link to comment
Share on other sites

A) You might (untested) be able to use the HTML <base> Tag.

 

B) You can use a configuration constant/variable/replaceable parameter in each link that gets the actual value of the http://domain.com/path at runtime or do a string replace/preg_replace at runtime to put in the actual value.

 

C) You can put in the http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] before every link

Link to comment
Share on other sites

Another possibility is to use output buffering.

 

Turn on output buffering to "hold" output (i.e. echo/print content) in memory until you are done running the page. Then before sending the output run a regex replacement against the content to do a replacement of all the image URLs to include the full path. Then, send the output to the browser.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.