Problem with file reading

This is a discussion on Problem with file reading within the PHP General forums, part of the PHP Programming Forums category; Hi, I'm a beginner and i'm still learning PHP and I got a problem: $file = "http://localhost/...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2007
Delta Storm
 
Posts: n/a
Default Problem with file reading

Hi,

I'm a beginner and i'm still learning PHP and I got a problem:




$file = "http://localhost/test_folder/test1.txt"; //I have also
tried "test_folder/test1.txt" and "text1.txt"


$fh = fopen($file, "r") or die("Could not open file!");


$data = fread($fh, filesize($file)) or die ("Could not read file!");


fclose($fh);


echo $data;


The file exist, I'm using apache server on my PC for practicing and the
file is located in the servers root folder on the subfolder "test_folder".

Help would be greatly appreaciated

Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 01-05-2007
Stut
 
Posts: n/a
Default Re: [PHP] Problem with file reading

Delta Storm wrote:
> I'm a beginner and i'm still learning PHP and I got a problem:
>
> $file = "http://localhost/test_folder/test1.txt"; //I have
> also tried "test_folder/test1.txt" and "text1.txt"
>
> $fh = fopen($file, "r") or die("Could not open file!");
>
> $data = fread($fh, filesize($file)) or die ("Could not
> read file!");
>
> fclose($fh);
>
> echo $data;
>
> The file exist, I'm using apache server on my PC for practicing and
> the file is located in the servers root folder on the subfolder
> "test_folder".


Is there a reason why you're trying to access it through a URL? If not,
please don't.

The file path needs to be relative to the current file. So if your PHP
file is in the root, "test_folder/test1.txt" should work. If your PHP
file is in a folder named code in the root, "../test_folder/text1.txt"
should work.

Also, depending on your PHP version you're probably better off using
file_get_contents instead of fopen/fread/fclose.

-Stut
Reply With Quote
  #3 (permalink)  
Old 01-05-2007
Stut
 
Posts: n/a
Default Re: [PHP] Problem with file reading

Roman Neuhauser wrote:
> # stuttle@gmail.com / 2007-01-05 16:34:41 +0000:
>
>> Delta Storm wrote:
>>
>>> I'm a beginner and i'm still learning PHP and I got a problem:
>>>
>>> $file = "http://localhost/test_folder/test1.txt"; //I have
>>> also tried "test_folder/test1.txt" and "text1.txt"
>>>
>>> $fh = fopen($file, "r") or die("Could not open file!");
>>>
>>> $data = fread($fh, filesize($file)) or die ("Could not
>>> read file!");
>>>
>>> fclose($fh);
>>>
>>> echo $data;
>>>
>>> The file exist, I'm using apache server on my PC for practicing and
>>> the file is located in the servers root folder on the subfolder
>>> "test_folder".
>>>

>> Is there a reason why you're trying to access it through a URL? If not,
>> please don't.
>>
>> The file path needs to be relative to the current file. So if your PHP
>> file is in the root, "test_folder/test1.txt" should work. If your PHP
>> file is in a folder named code in the root, "../test_folder/text1.txt"
>> should work.
>>

>
> Arent' you confusing this with something else? fopen() is affected
> by the current working directory of the process calling it. What kind of
> "root" are you talking about? The filesystem root, "/"?


The OP referred to the "servers root folder". Now stop nit-picking and
go back to quoting standards.

-Stut
Reply With Quote
  #4 (permalink)  
Old 01-05-2007
Stut
 
Posts: n/a
Default Re: [PHP] Problem with file reading

Roman Neuhauser wrote:
> # stuttle@gmail.com / 2007-01-05 17:17:46 +0000:
>
>> Roman Neuhauser wrote:
>>
>>> # stuttle@gmail.com / 2007-01-05 16:34:41 +0000:
>>>
>>>> Delta Storm wrote:
>>>>
>>>>> I'm a beginner and i'm still learning PHP and I got a problem:
>>>>>
>>>>> $file = "http://localhost/test_folder/test1.txt"; //I have
>>>>> also tried "test_folder/test1.txt" and "text1.txt"
>>>>>
>>>>> $fh = fopen($file, "r") or die("Could not open file!");
>>>>>
>>>>> The file exist, I'm using apache server on my PC for practicing and
>>>>> the file is located in the servers root folder on the subfolder
>>>>> "test_folder".
>>>>>
>>>>>
>>>> Is there a reason why you're trying to access it through a URL? If not,
>>>> please don't.
>>>>
>>>> The file path needs to be relative to the current file. So if your PHP
>>>> file is in the root, "test_folder/test1.txt" should work. If your PHP
>>>> file is in a folder named code in the root, "../test_folder/text1.txt"
>>>> should work.
>>>>
>>> Arent' you confusing this with something else? fopen() is affected
>>> by the current working directory of the process calling it. What kind of
>>> "root" are you talking about? The filesystem root, "/"?
>>>

>> The OP referred to the "servers root folder". Now stop nit-picking and
>> go back to quoting standards.
>>

>
> I'm not nitpicking. If the webserver process (assuming mod_php) runs in
> a different directory, fopen("relative/to/documentroot/path.txt") won't
> help him at all, and as far as I can tell, it's quite common for web
> servers to run with pretty much any cwd, often / or /var/empty (these
> are local filesystem paths).


Ok, let me clarify...

Using mod_php with Apache will make the current directory the directory
that contains the script being requested. I am assuming that the OP is
trying to access the file from the script that is being requested and
not an included file in a different directory - I should have been
clearer on that.

I never said anything about being relative to the document root - you
pulled that out of somewhere yourself.

Happy now?

-Stut
Reply With Quote
  #5 (permalink)  
Old 01-05-2007
Roman Neuhauser
 
Posts: n/a
Default Re: [PHP] Problem with file reading

# stuttle@gmail.com / 2007-01-05 16:34:41 +0000:
> Delta Storm wrote:
> >I'm a beginner and i'm still learning PHP and I got a problem:
> >
> > $file = "http://localhost/test_folder/test1.txt"; //I have
> >also tried "test_folder/test1.txt" and "text1.txt"
> >
> > $fh = fopen($file, "r") or die("Could not open file!");
> >
> > $data = fread($fh, filesize($file)) or die ("Could not
> >read file!");
> >
> > fclose($fh);
> >
> > echo $data;
> >
> >The file exist, I'm using apache server on my PC for practicing and
> >the file is located in the servers root folder on the subfolder
> >"test_folder".

>
> Is there a reason why you're trying to access it through a URL? If not,
> please don't.
>
> The file path needs to be relative to the current file. So if your PHP
> file is in the root, "test_folder/test1.txt" should work. If your PHP
> file is in a folder named code in the root, "../test_folder/text1.txt"
> should work.


Arent' you confusing this with something else? fopen() is affected
by the current working directory of the process calling it. What kind of
"root" are you talking about? The filesystem root, "/"?

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991
Reply With Quote
  #6 (permalink)  
Old 01-05-2007
Roman Neuhauser
 
Posts: n/a
Default Re: [PHP] Problem with file reading

# stuttle@gmail.com / 2007-01-05 17:17:46 +0000:
> Roman Neuhauser wrote:
> ># stuttle@gmail.com / 2007-01-05 16:34:41 +0000:
> >>Delta Storm wrote:
> >>>I'm a beginner and i'm still learning PHP and I got a problem:
> >>>
> >>> $file = "http://localhost/test_folder/test1.txt"; //I have
> >>>also tried "test_folder/test1.txt" and "text1.txt"
> >>>
> >>> $fh = fopen($file, "r") or die("Could not open file!");
> >>>
> >>>The file exist, I'm using apache server on my PC for practicing and
> >>>the file is located in the servers root folder on the subfolder
> >>>"test_folder".
> >>>
> >>Is there a reason why you're trying to access it through a URL? If not,
> >>please don't.
> >>
> >>The file path needs to be relative to the current file. So if your PHP
> >>file is in the root, "test_folder/test1.txt" should work. If your PHP
> >>file is in a folder named code in the root, "../test_folder/text1.txt"
> >>should work.

> >
> >Arent' you confusing this with something else? fopen() is affected
> >by the current working directory of the process calling it. What kind of
> >"root" are you talking about? The filesystem root, "/"?

>
> The OP referred to the "servers root folder". Now stop nit-picking and
> go back to quoting standards.


I'm not nitpicking. If the webserver process (assuming mod_php) runs in
a different directory, fopen("relative/to/documentroot/path.txt") won't
help him at all, and as far as I can tell, it's quite common for web
servers to run with pretty much any cwd, often / or /var/empty (these
are local filesystem paths).

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991
Reply With Quote
  #7 (permalink)  
Old 01-05-2007
Roman Neuhauser
 
Posts: n/a
Default Re: [PHP] Problem with file reading

# stuttle@gmail.com / 2007-01-05 17:40:49 +0000:
> Using mod_php with Apache will make the current directory the directory
> that contains the script being requested. I am assuming that the OP is
> trying to access the file from the script that is being requested and
> not an included file in a different directory - I should have been
> clearer on that.
>
> I never said anything about being relative to the document root - you
> pulled that out of somewhere yourself.


You're right, sorry for the noise!

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:45 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0