473,811 Members | 4,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

$fileID = fopen('myfile.t xt', 'r'); - Fails when attempting to find file in same directory as script

Using Linux/PHP 4.3.2 CLI:

[PHP]$fileID = fopen('myfile.t xt', 'r'); // WORKS JUST FINE [/PHP]

Using Linux/PHP 4.3.8 CLI:

[PHP]$fileID = fopen('myfile.t xt'); // THROWS WARNING 'no such file or
directory "myfile.txt " [/PHP]

Using Windows XP/PHP 5.0.4 CLI:

[PHP]$fileID = fopen('myfile.t xt'); // THROWS WARNING 'no such file or
directory "myfile.txt "[/PHP]

In all three cases this script, let's call it myscript.php, resides in
the exact same directory as "myfile.txt ". Both scripts have
permissions of 0644, same owner, same group (Unix).

For the Windows problem I managed to use a coding solution that will
use the absolute path that is generated within $_SERVER['PHP_SELF']
(which apparently only occurs within Windows - have never seen that
variable populated when using a UNIX-based OS):

[PHP]
if ($_ENV['windir'] || $_SERVER['windir']) {
$path = substr($_SERVER['PHP_SELF'], 0,
strrpos($_SERVE R['PHP_SELF'], '/') + 1);
} else {
$path = '';
}
[/PHP]

This, however, will produce no absolute path information for UNIX-based
OS using "myscript.p hp" to access "myfile.txt ". So using older
versions of PHP as recent as PHP 4.3.8 within UNIX (Linux), "$fileID =
fopen('myfile.t xt', 'r');" you have to extract from $argv[0], provided
you are using CLI PHP and you just happen to NOT be in the directory
where "myscript.p hp" is housed, otherwise, you obviously get no
absolute path info to locate "myfile.txt ", and yes, when tested in PHP
4.3.11 and PHP 5.0.4, I constantly got the warning "No such file or
directory: "myfile.txt "" even though it's clearly there with
myscript.php.

Bottom line: Is there a foolproof way of always obtaining the absolute
path of something using CLI PHP; or... what other alternatives have you
all come up with using CLI PHP to always reliably locate a file that
happens to be in the same directory as your script?

Thanx
Phil

Aug 11 '05 #1
3 3007
*** comp.lang.php wrote/escribió (11 Aug 2005 09:20:22 -0700):
Bottom line: Is there a foolproof way of always obtaining the absolute
path of something using CLI PHP


I think __FILE__ should work.
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 12 '05 #2
WOW! Thanx that was dead on perfect!
Phil

Aug 12 '05 #3
On 12 Aug 2005 08:25:06 -0700, comp.lang.php wrote:
WOW! Thanx that was dead on perfect!
Phil


Just _what_ are you nattering on about?
Oh, I see the problem:
X-Trace: posting.google. com

http://groups.google.com/googlegroup...html#summarize
http://oakroadsystems.com/genl/unice.htm#quote
Aug 12 '05 #4

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

Similar topics

1
6568
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage returns false, however, on the first time the function is run, but returns true the second time it is run. Can anyone suggest why fwrite only works the second time?
28
33197
by: Sathyaish | last post by:
If fopen fails, is there a way to know why?
1
1419
by: msnews.microsoft.com | last post by:
Help! I'm working with a mixed mode project in which some existing code is calling fopen. ie., void SomeMethod(char* fileName) { FILE* filePointer; filePointer = fopen(fileName, "r");
7
4662
by: Efi Merdler | last post by:
Hi, I created a very simple application that reads and writes to a file, I was wondering if fopen when opening a file for write access blocks this file, i.e. when another user runs this script the script will halt until the first user closes its file handle. If not how can I change php behavior to block such access? Thank you, Efi
0
10644
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9201
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.