473,395 Members | 2,222 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,395 software developers and data experts.

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

Using Linux/PHP 4.3.2 CLI:

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

Using Linux/PHP 4.3.8 CLI:

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

Using Windows XP/PHP 5.0.4 CLI:

[PHP]$fileID = fopen('myfile.txt'); // 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($_SERVER['PHP_SELF'], '/') + 1);
} else {
$path = '';
}
[/PHP]

This, however, will produce no absolute path information for UNIX-based
OS using "myscript.php" to access "myfile.txt". So using older
versions of PHP as recent as PHP 4.3.8 within UNIX (Linux), "$fileID =
fopen('myfile.txt', '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.php" 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 2974
*** 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
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...
28
by: Sathyaish | last post by:
If fopen fails, is there a way to know why?
1
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...

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.