This should be so simple but I'm a bit stumped as to the best way to approach this.
I am building a PHP plugin so I can't hard code any paths. I also need to consider http protocols (http / https). I use GD Lib to create and save an image. My directory structure looks like this:
public_html > php_plugin > php_classes > php_class.php
public_html > php_plugin > saved_images
The plugin is installed in public_html
. I run code to save the image in php_class.php
. Here I save the image like so: '../saved_images/imagename.jpg'
. This works fine and the image is saved in the correct directory.
I then return this path via AJAX to display the image. And, obviously this path fails to resolve and I get a broken image link: http://www.website.com/saved_images/imagename.jpg
It should look like: http://www.website.com/php_plugin/saved_images/imagename.jpg
So in my PHP class where I set up the save path how do I build up a path that I can return with AJAX, bearing in mind this is a plugin and could be used on many different sites.
EDIT:
For the record I know I can use $_SERVER['SERVER_NAME']
and $_SERVER['SERVER_PROTOCOL']
but based on these SO questions (PHP $_SERVER['HTTP_HOST'] vs. $_SERVER['SERVER_NAME'], am I understanding the man pages correctly? and PHP Get Site URL Protocol - http vs https) I think this approach will be quite unreliable as the server configs where the plugin will be hosted will be an unknown.