FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Reading a file on same server
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Reading a file on same server [message #182368 is a reply to message #182362] Wed, 31 July 2013 16:09 Go to previous messageGo to previous message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma:
Senior Member
On 31/07/13 13:13, Bruce Lee wrote:
> HI
>
> Suppose I have a php file "read_file.php" that is used to read .txt files, the structure of the folder/files are:
>
> /www/read_file.php
> /www/public/upload/
>
> I was wondering which will be faster and why in PHP..
>
> Method 1:
> read file using relative path
>
> Eg: get_file_contents("public/upload/test.txt");
>
>
> Method 2:
> read file using URL (note that the server which is making the request and the server where the file is being read is same)
>
> Eg: get_file_contents("http://mydomain.com/public/upload/test.txt");
>
>
> Can someone explain me which is faster and why?

Compare what would happen:

Method 1:
1. PHP opens a file.
2. Reads file.
3. Close file.

Method 2:
1. PHP makes a handshake with the webserver
2. Web server opens file.
3. Web server opens log file (or it's already open, depends on config).
4. Web server reads file.
5. Web server sends file.
6. Web server writes to log.
7. PHP receives data

Handshakes are usually time consuming and as the web server will do
other things too, it will cause overhead.

You could compare the whole with reading a book, I'll present two
methods and you pick which one is faster:

Method 1:
You read a book.

Method 2:
You ask your friend to read a book, write it down on paper and you read
the written papers.

And if you doubt us, do as Thomas suggests, measure the time consumed,
test it say 1000 times each method, compare the times.

There is another aspect to it too, error handling will be more
difficult, as you don't really know why the web server suddenly gives
you a 404, is the file really gone or just that the web server ain't
allowed to access it or someone changed the DocumentRoot.

Also the more you do over the net instead of directly, it's more
difficult to follow what happens in your code and it becomes difficult
to maintain the code in a proper way, I have seen such code out in the
live, just for someone thought it was simpler to call another script
than write the function in a way that you could include the code and use
it directly.

--

//Aho
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Why type hint when you can type cast
Next Topic: Newline (cr/lf) in imagestring
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 15:38:55 GMT 2024

Total time taken to generate the page: 0.04857 seconds