#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    3

    Help on require_once()


    Hi!

    I am developing an application using PHP in which one of my calling scripts has a statement in it require_once('reports/prn_intake.php'); in a function create_report(). Now as can be seen my calling script in in the root directory and my report script is in /reports directory in the root. In side the report script I have some hard coded paths for some image files that are inside the /reports directory. Therefore when my calling script calls for this function create_report(), the /reports/prn_intake.php is executed but gives errors because of the hard coded path of the image file inside the report script. The same report is also being used independently through a menu option to run the report and therefore I can not keep two versions of the same report script with changed image paths. How do I tell my prn_intake.php script that it has been called from one script or another based on which I can change the paths inside the report script.

    Hope I am making sense in asking this query. I need help in knowing any other way of executing a script by taking the control to that script where it is located rather than using require_once() which actually inserts the script in my calling script at that point. Correct me if I am wrong. Would greatly appreciate some help in this regard.

    Thanks in advance!
    Mozart66
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    not sure if I understand the problem. So is the problem that you have relative image paths in your report script? And that those paths break when the current directory changes due to the script being included?

    In that case, simply use absolute paths (which is generally a good idea, by the way). Use __DIR__ or dirname(__FILE__) to get the absolute path of the directory your script file is in. This path is always the same, regardless of how and where you include the script.

    Comments on this post

    • dmittner agrees : A bit of extra code to use absolute paths but much less prone to confusion than relative paths.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    33
    Rep Power
    3
    Thanks! It worked!

    Comments on this post

    • Jacques1 disagrees : Please don't close your threads. Often times, other users have additional information and comments. They can't post them if you immediately lock the topic.

IMN logo majestic logo threadwatch logo seochat tools logo