473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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,
$printAnErrorMessageAtTheEndIfTheFileIsStillUnfoun d="y") {
$tryThisDir = "mcControlPanelTemplates/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
if (!$theFileIsLoaded) {
$tryThisDir = "mcCostin/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcFiles/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcGet/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcGlobalEvents/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcGlobalVariables/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcHtml/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcImages/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcKernel/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcProcessEach/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcShowAdmin/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcShowPublic/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcUtility/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "mcArrangements/";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "./";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "../";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "../../";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded) {
$tryThisDir = "../../../";
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include_once($tryThisFile);
$tryThisFile = $tryThisFile.".php";
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
$tryThisFile = str_replace("/Mc", "/mc", $tryThisFile);
if (!$theFileIsLoaded) $theFileIsLoaded =
@include_once($tryThisFile);
}
if (!$theFileIsLoaded &&
$printAnErrorMessageAtTheEndIfTheFileIsStillUnfoun d == "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 2638
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,
$printAnErrorMessageAtTheEndIfTheFileIsStillUnfoun d="y") {


LOL!

André Næss
Jul 17 '05 #3
André Næss <an*********************@ifi.uio.no> wrote in message news:<bm**********@maud.ifi.uio.no>...
lawrence:
function import($name,
$printAnErrorMessageAtTheEndIfTheFileIsStillUnfoun d="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******@geocities.com> wrote in message
news:da**************************@posting.google.c om...
André Næss <an*********************@ifi.uio.no> wrote in message

news:<bm**********@maud.ifi.uio.no>...
lawrence:
function import($name,
$printAnErrorMessageAtTheEndIfTheFileIsStillUnfoun d="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
'mcMySqlDatabase.php'" and then "The software needs a file called
'mcShowCommentsPublic.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
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...
6
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...
0
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...
1
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...
6
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...
3
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...
5
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,...
9
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...
12
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.