Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10313 > unrolled thread

Reading a file on same server

Started byBruce Lee <phpzendguru@gmail.com>
First post2013-07-31 04:13 -0700
Last post2013-08-01 21:28 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  Reading a file on same server Bruce Lee <phpzendguru@gmail.com> - 2013-07-31 04:13 -0700
    Re: Reading a file on same server The Natural Philosopher <tnp@invalid.invalid> - 2013-07-31 12:40 +0100
    Re: Reading a file on same server Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-07-31 15:13 +0200
    Re: Reading a file on same server "J.O. Aho" <user@example.net> - 2013-07-31 18:09 +0200
      Re: Reading a file on same server Luuk <luuk@invalid.lan> - 2013-08-01 21:28 +0200

#10313 — Reading a file on same server

FromBruce Lee <phpzendguru@gmail.com>
Date2013-07-31 04:13 -0700
SubjectReading a file on same server
Message-ID<94992e21-fd7e-47b5-868f-6735808e50a4@googlegroups.com>
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?


Thanks

[toc] | [next] | [standalone]


#10314

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-07-31 12:40 +0100
Message-ID<ktat2o$51i$1@news.albasani.net>
In reply to#10313
On 31/07/13 12: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?
>
The first one. Unless PHP is exceptionally smart the second one will invoke a recursive call to the (apache) server to deliver the file spawning another process and making and destroying a socket in the process.



> Thanks


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#10317

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-07-31 15:13 +0200
Message-ID<3235081.K99DxYmB1T@PointedEars.de>
In reply to#10313
Bruce Lee wrote:
^^^^^^^^^
Doubtful.  Bruce Lee would have known the answer, having instructed Chuck 
Norris who, as we all know, can write 100 LOCs of PHP with one finger in 
less than one minute.

> 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?

Yes.  But if you thought harder about what needs to happen in each case to 
get the file contents (i.e., do your own homework), you could have explained 
it yourself.  That said, you could have just measured it:

<http://php.net/microtime>


PointedEars
-- 
When all you know is jQuery, every problem looks $(olvable).

[toc] | [prev] | [next] | [standalone]


#10319

From"J.O. Aho" <user@example.net>
Date2013-07-31 18:09 +0200
Message-ID<b5sr4vFm4kuU1@mid.individual.net>
In reply to#10313
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

[toc] | [prev] | [next] | [standalone]


#10325

FromLuuk <luuk@invalid.lan>
Date2013-08-01 21:28 +0200
Message-ID<qfpqca-qdl.ln1@luuk.invalid.lan>
In reply to#10319
On 31-07-2013 18:09, J.O. Aho wrote:
> 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.
>

And what about someone else going to:
http://mydomain.com/public/upload/test.txt

With the first method it's possible to store your docs where they are 
not reachable directly from the outside world.....

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web