php include files problem

Discussion in 'Software' started by charco, Jul 16, 2008.

  1. charco

    charco Private E-2

    Hi MGs

    I am trying to use php include files across directories. The inclusion I have no problems with, but the actual files that are included lose their src referencing when they are written to the parent file. Is there anyway round this?

    Let me explain the problem more graphically.

    I have an index file the is going ot call up a menu.php file (from a menu directory) and a body.php file from a body directory.

    However, the menu.php file has references to images from a directory "/menus/img/"
    When the menu.php file is written into the index file all of the references between menu.php and the images from menus/img/ are of course incorrect and the images do not show up in the header of the index file.

    Same story for the body.php which src's the body/img/ file.

    There are many images and rollovers in these sub-directories so changing coding by hand is not really an option.

    Does anyone have any idea how I can work around this?
     
  2. PC-XT

    PC-XT Master Sergeant

    I have directories like you say, and have problems with them sometimes. The best way I've found to fix the problem you are talking about is to place a chdir statement at the top of each include file to fix the references (by giving an absolute path to the directory containing the include script). However, this doesn't always work, for instance, if those functions are later called after another chdir statement. Then you will need a chdir statement in the function. If you have to edit the source file, I find the best way is to make a PHP script that replaces the strings in each source file, like 'img/' with 'menus/img/'. You can also just use a text editor that does replacing if you don't want to write a script. PHP can find the places where the attributes are, and just add the path to them.

    Included files are inserted in the calling script, and use it's properties, such as current directory. Calling chdir will change the current directory for the whole script, including the includes. If the includes are just lists of functions, each function that handles these paths will need a fix. (At least the ones that access the file.)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds