xstortionist Posted May 19, 2011 Share Posted May 19, 2011 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" ? Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/ Share on other sites More sharing options...
Stooney Posted May 19, 2011 Share Posted May 19, 2011 It's something that should have been designed for originally. You could probably ghetto fix it with javascript by prepending the path to all src attributes on img tags. But that's pretty ghetto. Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1217763 Share on other sites More sharing options...
Pikachu2000 Posted May 19, 2011 Share Posted May 19, 2011 If I understand what you're trying to do, this should be all you need <img src="/images/pic.png"> Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1217788 Share on other sites More sharing options...
xstortionist Posted May 19, 2011 Author Share Posted May 19, 2011 The logic behind what i'm trying to do is as follows: Replace all instances of "images/" with "http://www.mydomain.com/images/" Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1217813 Share on other sites More sharing options...
Pikachu2000 Posted May 19, 2011 Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1217845 Share on other sites More sharing options...
xstortionist Posted May 20, 2011 Author Share Posted May 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1218011 Share on other sites More sharing options...
PFMaBiSmAd Posted May 20, 2011 Share Posted May 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1218016 Share on other sites More sharing options...
Pikachu2000 Posted May 20, 2011 Share Posted May 20, 2011 OK, now it makes sense. It wasn't clear that this was actually for the body of an email. Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1218026 Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236879-php-newb-how-can-i-get-absolute-path-for-images/#findComment-1218093 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.