Jump to content

How bad is fread() etc?


marklarah

Recommended Posts

Hi!

 

I run a site the requires users to access largeish files, for download as well for streaming to the browser.

 

It's fairly active, so assuming the worst, how bad is getting php to read the files that would be stored outside of the webroot and then getting it to echo it to a page dynamically for the browser to then read?

 

Currently, I just have the files stored accessible to the web, but a htaccess protects them via requiring a cookie  but also requires the referrer to be my site. Now I'm not totally stupid and realise those are both easily spoofed, so I'm looking for a better solution.

How do the file hosting sites do it? Using a token system has to involve PHP or some other server side scripting, but then getting the server to read the file with fread() or some similar equivalent would be resource heavy, no? Is that the best way to do it?

Link to comment
Share on other sites

How do the file hosting sites do it? Using a token system has to involve PHP or some other server side scripting, but then getting the server to read the file with fread() or some similar equivalent would be resource heavy, no? Is that the best way to do it?

 

What are you trying to achieve? As for file hosting sites they use the tokens to determine wether the user is real and the user status. Since the services always return a file its goddamn heavy on the servers, thats why they've got plenty of them. And if I understood you correctly you are returning a file that contains text (or html or what ever)  and renders it to browser, it really doesnt matter since the client must anyhow download all the data (but for small optimization you could always cache the page(s) on the server and skip the readfile part in whole).

Link to comment
Share on other sites

but caching u would require some shared memory resources otherwise your back to file caching.

 

But the OP question was very vague, than he elaborates what he's trying to attempt in his last post.

how do download sites do it if not by reading the file? There has to be a way of controlling download speed etc

 

and by his posts, there is no real concept of what file functions do. and how do throtthle bandwidth.

 

1) learn to code

2) Post code yer having probs with

 

otherwise this thread will just be forgotten in a few days

 

Link to comment
Share on other sites

How do the file hosting sites do it? Using a token system has to involve PHP or some other server side scripting, but then getting the server to read the file with fread() or some similar equivalent would be resource heavy, no? Is that the best way to do it?

 

What are you trying to achieve? As for file hosting sites they use the tokens to determine wether the user is real and the user status. Since the services always return a file its goddamn heavy on the servers, thats why they've got plenty of them. And if I understood you correctly you are returning a file that contains text (or html or what ever)  and renders it to browser, it really doesnt matter since the client must anyhow download all the data (but for small optimization you could always cache the page(s) on the server and skip the readfile part in whole).

 

No, they're likely to be video files, and when I say echoing to the browser, that is infact what reading the file does, regardless of file type.

but caching u would require some shared memory resources otherwise your back to file caching.

 

But the OP question was very vague, than he elaborates what he's trying to attempt in his last post.

how do download sites do it if not by reading the file? There has to be a way of controlling download speed etc

 

and by his posts, there is no real concept of what file functions do. and how do throtthle bandwidth.

 

1) learn to code

2) Post code yer having probs with

 

otherwise this thread will just be forgotten in a few days

 

 

Actually I've been coding for 5 years now, but heavy download sites isn't something I've ever needed to dabble with until now.

 

My simple questions still remains unanswered: isn't using fread/readfile or something of the sort a bad hog on the server, and what are the alternatives

Link to comment
Share on other sites

A file has to be read in order to be served, and this will take a certain amount of resources, regardless of which method you choose to read and/or serve it.  Some functions will read and load the entire file into memory (like file or file_get_contents), others like fread can read and put into memory a file as little as 1 byte at a time. 

 

re: limiting download speeds with php

 

Interesting question, never really seen that come up before...not really sure what all you would do to limit download speeds with php...sure, you can simulate/fake it easy enough using one of the above reading functions and having php sleep a bit in intervals and flush the output buffer, but IMO that's not really the same thing.  Not sure true connection speed capping is even possible with php.  Maybe it is possible using sockets.  I'm thinking as far as limiting download speeds, you would probably use something like php to create a system (like membership system) to determine which speed a user should be downloading at, and then employing something else on the server to actually limit the connection speed..a shell script or some other server command/setting or something, not sure. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.