473,563 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strategies for including files when you're not allowed to make assumptions about directory names

2 Questions:

1.) Can anyone think of a way to speed up this function? It is
terribly slow. I plan to reduce the number of directories to 3, which
I guess will speed it up in the end.

2.) This seems to be the brute force method, and I'm wondering if
anyone can think of a better strategy. I've been asked to make life
easy on graphic designers by not making assumptions about where I
might find the files that the software might need to run. The files
might be in the same directory as the main file, or in directories
above, or in ones below. The person may have remembered to put a
".php" at the end of the file name, or maybe not. Here I simply go
looking. Is there a more graceful strategy than this? Some kind of
regular expression thing maybe?


function import($name,
$printAnErrorMe ssageAtTheEndIf TheFileIsStillU nfound="y") {
$tryThisDir = "mcControlPanel Templates/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
if (!$theFileIsLoa ded) {
$tryThisDir = "mcCostin/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcFiles/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcGet/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcGlobalEv ents/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcGlobalVariab les/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcHtml/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcImages/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcKernel/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcProcessE ach/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcShowAdmi n/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcShowPubl ic/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcUtility/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "mcArrangem ents/";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "./";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "../";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "../../";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded) {
$tryThisDir = "../../../";
$tryThisFile = $tryThisDir.$na me;
$theFileIsLoade d = @include_once($ tryThisFile);
$tryThisFile = $tryThisFile.". php";
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoa ded) $theFileIsLoade d =
@include_once($ tryThisFile);
}
if (!$theFileIsLoa ded &&
$printAnErrorMe ssageAtTheEndIf TheFileIsStillU nfound == "y") echo
"Awful sorry, but right here the software says it needs the file or
class $name, but it is unable to find it.";
}
Jul 17 '05 #1
7 2659
Hi Lawrence,
1.) Can anyone think of a way to speed up this function?
It is terribly slow. I plan to reduce the number of
directories to 3, which I guess will speed it up in the end.


Don't know why it's slow at the first sight - accessing
the filesystem (using "include" or "require") is not very
expensive, especially if the file does not even exist.

The overall execution time of your code depends on what
exactly is contained inside the included file(s).

This checking is done for every single request, right?

In my opinion it would make sense to check the file's
location only once per session, and store it, using a
session-persistent variable.

Greetings from Frankfurt / Germany,

Fabian Wleklinski
Jul 17 '05 #2
lawrence:
function import($name,
$printAnErrorMe ssageAtTheEndIf TheFileIsStillU nfound="y") {


LOL!

André Næss
Jul 17 '05 #3
André Næss <an************ *********@ifi.u io.no> wrote in message news:<bm******* ***@maud.ifi.ui o.no>...
lawrence:
function import($name,
$printAnErrorMe ssageAtTheEndIf TheFileIsStillU nfound="y") {


LOL!

André Næss


One gets tired of $printError="y" after a while. But you're right
about the needless length.
Jul 17 '05 #4
"lawrence" <lk******@geoci ties.com> wrote in message
news:da******** *************** ***@posting.goo gle.com...
André Næss <an************ *********@ifi.u io.no> wrote in message

news:<bm******* ***@maud.ifi.ui o.no>...
lawrence:
function import($name,
$printAnErrorMe ssageAtTheEndIf TheFileIsStillU nfound="y") {


LOL!

André Næss


One gets tired of $printError="y" after a while. But you're right
about the needless length.


Without sounding like a killjoy but this sounds like a recipe for disaster.

What if your oh-so-important graphic designers update a file and place a
copy of it in a different directory? It's possible that your designers could
then end up giving you multiple copies of a file and then they'll start
blaming you for not checking file dates as part of your processing and
establishing the most up-to-date version. Maybe they'll then open/save a
file without changing it so you can't tell any more which is the most
up-to-date version. Maybe you'll then have to add a version line to each
file and check that ... but then they'll maybe forget to update that line as
well.

Good programming is about control. Controlling the computer and controlling
the flow of information around the computer. You shouldn't use include_once
in a file because you as a programmer should know that your logic dictates
it wouldn't be included twice (recursive functions excepted). It's the same
with everything in a script, you don't introduce functions that lose control
or create a situation that leads to it. Your graphic designers should be
told what to do, that this is the design standard and that's what you expect
them to follow. If that's not possible, then you need to manually move the
files these incompetent designers have misplaced so that you maintain the
control in your application. If others are going to be stupid, you'll have
to make up for their stupidity and spend your time doing it. If that's what
your client or employer prefers then so be it. I'm sure if you pointed out
that developer's time is more expensive than designers time they may back
your point of view.

Paulus
Jul 17 '05 #5
Carved in mystic runes upon the very living rock, the last words of
lawrence of comp.lang.php make plain:
1.) Can anyone think of a way to speed up this function? It is
terribly slow. I plan to reduce the number of directories to 3, which
I guess will speed it up in the end.


I don't know about faster, but a cleaner way would be to have an array of
directories and loop through it using is_readable(), and when you find it,
break out of the loop and include().

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #6
Alan Little <al**@n-o-s-p-a-m-phorm.com> wrote in message news:<Xn******* *************** ***@216.196.97. 132>...
Carved in mystic runes upon the very living rock, the last words of
lawrence of comp.lang.php make plain:
1.) Can anyone think of a way to speed up this function? It is
terribly slow. I plan to reduce the number of directories to 3, which
I guess will speed it up in the end.


I don't know about faster, but a cleaner way would be to have an array of
directories and loop through it using is_readable(), and when you find it,
break out of the loop and include().


I think you're right. I will rewrite it as you say.
Jul 17 '05 #7
"Paulus Magnus" <pa***********@ loves-spam.com> wrote in message
Without sounding like a killjoy but this sounds like a recipe for disaster.
What if your oh-so-important graphic designers update a file and place a
copy of it in a different directory? It's possible that your designers could
then end up giving you multiple copies of a file and then they'll start
blaming you for not checking file dates as part of your processing and
establishing the most up-to-date version. Maybe they'll then open/save a
file without changing it so you can't tell any more which is the most
up-to-date version. Maybe you'll then have to add a version line to each
file and check that ... but then they'll maybe forget to update that line as
well.
You raise a good point. Basically, the graphic designers could throw
in a file that has the same name as a file that I wrote, and the
results could be disaterous.

Good programming is about control. Controlling the computer and controlling
the flow of information around the computer. You shouldn't use include_once
in a file because you as a programmer should know that your logic dictates
it wouldn't be included twice (recursive functions excepted).
Um, if I don't include files till their needed, then when they are
needed I need to include them using include_once, because some other
function might have already included them. A graphic designer, working
in Dreamweaver, designing a site for a client, and using some of these
software tools we've developed as a convenient way to drop interactive
elements into the page, might write something like:

<div class="navBar">
<?php showNav(1); ?>
</div>

<div class="comments ">
<?php showComments(); ?>
</div>

Both these commands would need the database object, so both would
include_once(). Of course, I've no idea that the designer is going to
use either of these. They may only use one command that needs the
database object on the page.

It's the same
with everything in a script, you don't introduce functions that lose control
or create a situation that leads to it. Your graphic designers should be
told what to do, that this is the design standard and that's what you expect
them to follow.
The head of the project, who is a graphic designer, said to me,
"Listen, all this software is great, but I've got friends who'd like
to use parts of this system, but they don't want use the whole thing.
It's too much. A lot of times its too complicated for what's needed.
Is there a way we can break this up? Like, for instance, if one of my
friends just wants to use the comments software on their websites, but
not all the other junk, can they do that?"

Up till that point I had simply included every file when the page
loaded. Every file got loaded once, and I knew what directly every
file was in. But since that conversation with the lead guy, I've been
trying to figure out a way to allow "minimum installations." I want to
be able to say, okay, this software has 140 files but you only 7 of
them because all you want to do is run the comments.

Somehow I need to figure out a way that will allow this to work. (As
an architextural point, I've been breaking down the objects into very
small objects, again to allow the minimal of "minimal installations".

How to get a minimal installation? This was my first thought, and this
is what I've got working right now: There is a small kernel, 5 files
taking up 24k, that needs to be loaded always. A designer can put in
just the kernel, then attempt use whatever functions they wanted. The
kernel isn't enough to do what they want, but it is enough to print
intelligent error messages to guide them. The software would print a
string of error messages like, "The software needs the file
'mcMySqlDatabas e.php'" and then "The software needs a file called
'mcShowComments Public.php'". The graphic designer would add in
whatever files the software asks for. After 10 minutes, the software
has maybe asked for 9 additional files, beyond what is in the kernel.
There are no more error messages, because for the limited set of
abilities that the grapic desinger wants, this is all they need. Now
they've got the functionality they want, and with only 14 files
installed, instead of 140.

This is what I've got working, but had not thought of the problem you
point out: identically named files.
If that's not possible, then you need to manually move the
files these incompetent designers have misplaced so that you maintain the
control in your application. If others are going to be stupid, you'll have
to make up for their stupidity and spend your time doing it. If that's what
your client or employer prefers then so be it. I'm sure if you pointed out
that developer's time is more expensive than designers time they may back
your point of view.


I think you raised an excellent point about the multiple, identical
filenames. This product is still in beta and we are still testing a
lot of ideas and, frankly, we are making design mistakes like this. I
should say, I am making design mistakes like this. But we've got time
to make mistakes and test different ideas. We've given ourselves the
time to make mistakes.

My first reaction, upon hearing your concern, is to throw in a test
for files with the same name. If ever there are two files with the
same name, the software will just print an error, and explain the
situation. That should be enough to let the graphic designers know
what the problem is.

We don't want to be like PostNuke, and impose a monolithic answer on
how things should be set up and done.
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1639
by: khan | last post by:
this is my code, it is updating QTY on Hand when you make a new sale or if you got some product returned. Now as it is updating QTY when qty textbox lost focus if some one is going through records and whenever this qty text box will losee focus it will be fired and change the quantity in the product table. I want it, to be fired only ig user...
6
2102
by: GG | last post by:
Currently we have clientMail.dll in dot net. All the dot net exes use it to send e-mail. However, the above will not work with mcafee 8 unless adding into the exceptions all the apps exe. I would like only to make a clientMail.dll to exe so I can only add one exception to the list I can add public static void Main()
0
2089
by: Don | last post by:
I intermittently get a runtime Compilation Error that says 'The compiler failed with error code 2000'. It appears that a DLL cannot be found in the 'temporary asp.net files' directory. The Detailed Compiler Output is at the bottom of this post. This is a custom dll named UtilitiesINGR.dll that I put in the GAC. I added a machine.config...
1
1485
by: Shawn Mehaffie | last post by:
I have an application I want to be able to: 1) Store user specifc settings in ther "My Documents". 2) Store some information in "All Users" document directory. 3) I also want to be able to access program files that are installed in the "Program Files" directory. How can I get these locations from within a VB application.
6
3640
by: eBob.com | last post by:
How do you make a loop iterate early without using a GoTo? (I guess I've done too much structured programming and I really don't like using GoTos.) Here's my code ... For Each Thing As OFI In FileInfo If Thing.Displayed <> True Then GoTo Iterate 'skip this entry; try next one End If
3
2063
by: comp.lang.php | last post by:
I have a counter that evokes the "Three Strikes You're Out" rule.. if you make more than N mistakes it auto-resets to avoid flooding $_SESSION with attempt after attempt, etc. However, the counter never advances beyond 1! // HANDLE THE PART WHERE THE STUDENT INFORMATION WILL BE UPDATED OR SEARCHED if (is_array($_POST) &&...
5
1806
by: David Cartwright | last post by:
Hi all, I'm using a DataGrid to present a tabulated list (actually a list of users logged in to my phone system) - it seemed the most appropriate control. As stuff happens on the phone system, my program changes the colours of various lines - so if someone's on the phone their line would turn red, when they hang up it turns green, and so...
9
2197
by: xz | last post by:
What sense do h files make in c/cpp? I never thought about this question before. Does the existence of h files make sense at all? Why do we need to declare functions in h files and define/implement them in cpp?
12
8524
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h> int main(void)
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.