1

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.

Community
  • 1
  • 1
MP_Webby
  • 916
  • 1
  • 11
  • 35

1 Answers1

0

You are conflating two different types of path; the server filesystem path and the URL path. In your case they are similar, but depending on the server configuration these might be wildly different.

Since you write that you return a path to the image via AJAX, presumably your AJAX code must know the URL to the PHP script to be able to call it (something like http://example.com/php_plugin/index.php?). In this case you could just have PHP return only the filename and provide the rest of the path in your javascript.

timclutton
  • 12,682
  • 3
  • 33
  • 43