473,512 Members | 15,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find the path to a file stored at an unknown location on the filesystem?

13 New Member
Good Day to all,

Ahm, does anyone here know how can i make a function that get the filepath of a specific file?

I want to create a function like this:
- getFilePath('A_file_with_an_unknown_path.inc');
then the function will return the path of the file with an unknown path/directory.

Is this possible? If not, can anyone tell me how can I be able to include a file with an unknown filepath/directory?

- require_once('???/???/???/A_file_with_an_unknown_path.inc');

Please help.

Thank you in advance.
Jun 12 '08 #1
7 4689
Atli
5,058 Recognized Expert Expert
You could always use functions such as glob to search the file-system, but I wouldn't recommend it.

Why would you want to include a file with an unknown file-path?
Sounds like a very risky thing to do.
Jun 12 '08 #2
ghe
13 New Member
You could always use functions such as glob to search the file-system, but I wouldn't recommend it.

Why would you want to include a file with an unknown file-path?
Sounds like a very risky thing to do.
It is because the file directory of my program will be based on where the user installed my program in his/her PC. I cannot use such path like - "../../my_file.inc", because it is not an absolute path.
Jun 12 '08 #3
Atli
5,058 Recognized Expert Expert
If you know where the file is, relative to the file that is currently being executed, it's simple. You can use the __FILE__ constant with the pathname function to find the current directory and add to that. That would give the the absolute path to the file.
For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $relativePath = "path/to/file/filename.inc";
  3. $currentPath = dirname(__FILE__);
  4. include($currentPath ."/". $relativePath);
  5. ?>
  6.  
Would that work for you?
Jun 12 '08 #4
ghe
13 New Member
Thank you for this, but, sad to say I think it will not work. Because as far as I know, if __file__ is used, it will just tell the path of the current script.

It will not work if the case is like this:

A_file_with_an_unknown_path.inc file is located at "/unknown_root_dir/???/inc", and the file that will include the A_file_with_an_unknown_path.inc file is located at "/unknown_root_dir/???/script", then __file__ will then give a wrong filepath.
Jun 12 '08 #5
Atli
5,058 Recognized Expert Expert
Thank you for this, but, sad to say I think it will not work. Because as far as I know, if __file__ is used, it will just tell the path of the current script.

It will not work if the case is like this:

A_file_with_an_unknown_path.inc file is located at "/unknown_root_dir/???/inc", and the file that will include the A_file_with_an_unknown_path.inc file is located at "/unknown_root_dir/???/script", then __file__ will then give a wrong filepath.
Assuming that "/unknown_root_dir/???/" is the root for both directories, that is: if the relative path between the script that is being executed and the file_to_be_included is static... then you don't need an absolute path. Just use the static relative path.

Like, if I were to install your application at /var/www/app/, the paths would be:
/var/www/app/scripts/currentScript.php
/var/www/app/inc/file_to_be_included.inc

But if I were to install it at /home/username/app/, they would be:
/home/username/app/scripts/currentScript.php
/home/username/app/inc/file_to_be_included.inc

You could simply use the relative path: "../inc/file_to_be_included.inc".

Unless the 'inc' directory and the 'scripts' directory can be installed in separate root directories?
Jun 12 '08 #6
ghe
13 New Member
yes sir...they will be in two different directories...because the currentScript.php will be run through pgAgent, which will be located in "/usr/local/???/???/" path, while the file_to_be_included.inc will most likely be located in "/home/???/???" path.
Jun 13 '08 #7
Atli
5,058 Recognized Expert Expert
Ahh ok.
In that case you will either have to just search the entire file system, which is highly problematic (to say the least).

Or, you will have to device some method of letting the script know exactly where the other file is saved. That could be anything from creating a config file in a predefined location to a simple form asking for the location.

Personally, I would try the latter. You could have it save the location so it doesn't have to ask each time it is executed.
Jun 13 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

31
3869
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
70
4018
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
52
7458
by: paytam | last post by:
Hi all Can anyone tell me how can I check that a file exist or no.I mean when you use this commands FILE *fp; if(!fp) //Could not open the file doen't show why it can not open it,may be the...
4
4047
by: Dots | last post by:
I have a class library with a method called getpath(). I want to be able to get the full path of a folder and write some files to the (my_files_dir) folder. A console application will use this...
1
2719
by: marty.gagnon | last post by:
I have a xsl file that references a xml file using the document() function. I'm having trouble specifying the URI in the document() function of a xls stylesheet. I'm using java to transform the...
8
2306
by: inFocus | last post by:
Hello, I am new to python and wanted to write something for myself where after inputing two words it would search entire drive and when finding both names in files name would either copy or move...
8
11992
ashitpro
by: ashitpro | last post by:
Understanding Ext-2 file system:chapter 1 The first block in each Ext2 partition is never managed by the Ext2 filesystem, because it is reserved for the partition boot sector. The rest of the...
4
2819
by: Michel Rouzic | last post by:
I made a program that accepts as parameters an input file name that we'll call file1, and an output file name that we'll call file2, and opens on its own a configuration file called file3. My...
185
6855
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
0
7153
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...
0
7371
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,...
0
7432
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...
1
7093
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...
0
4743
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...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.