0

Here's my problem. I want to build a system where I don't need to worry about the paths of files. Right now I have all these "../" to reference different folders on the server.

I want to transform this:

<input type="image" src="../add_post.png" name="post" class="navigation_buttons" id="addPostButton" width="32" height="32">

into this:

<input type="image" src=<?php echo View::image("add_post.png");?> name="post" class="navigation_buttons" id="addPostButton" width="32" height="32">

The image function would just do something like:

public static function image($src) {
    return "../images/" . $src;
}

I know this will work because View's directory will never change relative the the folder images:

-> project
    -> libs
       -> View.php
    -> images
       -> add_post.png

The heart of my problems is really creating a resource system where I don't need to worry about paths. Just saying View:getImage("image name") would return the source path.

If there's a better way doing this, I'm all ears.

EDIT: My question is also about how to even allow for the html to correctly parse the PHP inline. The most I've gotten is an empty image. The html just doesn't load the image properly. Other times the image doesn't even show up.

Example of my attempt:

public static function get() {
    return '<input type="image" src="images/add_post.png" name="post" class="navigation_buttons" id="addPostButton" width="32" height="32">';
}

The HTML:

<div class="buttons">
<a href="pages/PostEntry.php">
    <?php echo View::get();?>
</a> 

No image button even shows up. I know for a fact that this is the correct image, as the other buttons I have are working properly with their respective HTML.

helsont
  • 4,347
  • 4
  • 23
  • 28

2 Answers2

1

Yes, at my job we have something like this (albeit in perl) but you could write your function to search the folders and return the absolute path (from web root) rather than the relative path.

public static function image($src) {  
    $webroot = '/www';
    $paths = array ( '/custom/', '/images/', '/images/subfolder/');  
    foreach $path ($paths){  
      if (file_exists($webroot . $path . $src)){  
         return( $path . $src);  
      }  
    }  
} 

This also allows us to have a folder priority so some templates can have different images for just a few images but fall back to the global '/images' for everything else.

If you absolutely must have the relative paths, you could pass in the current path (or an integer to represent folder-depth) so your function could send back the correct relative path after finding the image:
$cwd = getcwd();
Then count the slashes to find your folder depth and pass that in to tell your original "view" function how many ../ to prepend to the result.

Joe T
  • 2,300
  • 1
  • 19
  • 31
0

I found the true answer here:Apache: Inline PHP not working on Linux [SOLVED]

I needed to change all extensions to php not html to get this to work properly.

Community
  • 1
  • 1
helsont
  • 4,347
  • 4
  • 23
  • 28