Solved

Determine file dependencies of a script?

Posted on 2008-10-14
6
254 Views
Last Modified: 2013-12-15
Hello,

Anyone know a good way to determine what the file dependencies of a particular script are? I need not just the files immediately included in the file but ALL the files that file needs to run.

So, say I've got script A, and it includes class_B.php, and class B requires_once(file_C) and file_C needs file D.

Any way to figure out, from script A, what file D is?
0
Comment
Question by:dnatal
  • 3
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 300 total points
ID: 22718820
You could try calling get_included_files: http://www.php.net/manual/en/function.get-included-files.php
0
 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 200 total points
ID: 22720365
If you have a lot of scripts which you want to track down, then you can use a registered shutdown function invoked by using the auto_prepend_file facility to use get_included_files() and store then somewhere for each script.

Watch this space...
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 22720509
Something like the snippet below.

In your php.ini file you will have a line with ...

;auto_prepend_file=

If you save the snippet somewhere accessible to the web server (say Z:\LogIncludes.php) and change the ini file to be ...

auto_prepend_file=Z:/LogIncludes.php

Every script you run will have the includes logged for you.

If you run the file by itself, it should also log!


Z:\Usage>dir /s /b
Z:\Usage\58d404442823079089828b569b40f84c.log

Z:\Usage>type 58d404442823079089828b569b40f84c.log
Z:\logIncludes.php

array (
  0 => 'Z:\\logIncludes.php',
)



<?php
function logIncludes() {
	// Build string containing filename and included files.
	$s_Data = $_SERVER['PHP_SELF'] . PHP_EOL . PHP_EOL . var_export(get_included_files(), True);
 
	// Use a hash of the filename to store the data.
	// You will need to amend the path below to somewhere useful for you.
	$s_LogFile = 'Z:/Usage/' . md5($_SERVER['PHP_SELF']) . '.log';
 
	// Make sure the directory exists.
	mkdir(dirname($s_LogFile), 0777, True);
 
	// Log the file if it doesn't already exist.
	if (!file_exists($s_LogFile)) {
		file_put_contents($s_LogFile, $s_Data);
	}
}
 
register_shutdown_function('logIncludes');

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 22720516
I use a hash as this will reduce the complexity of the filenames in subdirectories. You COULD just save a log file next to each script, but I don't know if that is appropriate for you.


0
 
LVL 2

Author Comment

by:dnatal
ID: 22736050
get_included_files() is what I was looking for. Kinda wish it was a multidimensional array so I could see the dependency structures, or even just have parent file as array key, but it'll work.

All great answers and I really appreciate all the help.
0
 
LVL 2

Author Closing Comment

by:dnatal
ID: 31506040
Really appreciate the help. Split the points because they're both valid answers.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now