marklarah Posted December 24, 2010 Share Posted December 24, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/ Share on other sites More sharing options...
laffin Posted December 24, 2010 Share Posted December 24, 2010 no real reason to use fread in that situation. prolly readfile is a better solution. bout hard part is determining mime type for the browser to handle php.net has several examples of this: http://php.net/manual/en/function.readfile.php Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/#findComment-1151028 Share on other sites More sharing options...
marklarah Posted December 24, 2010 Author Share Posted December 24, 2010 Ok readfile, whatever. How bad is using readfile though over and over again, won't that hammer the server? Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/#findComment-1151089 Share on other sites More sharing options...
marklarah Posted December 25, 2010 Author Share Posted December 25, 2010 how do download sites do it if not by reading the file? There has to be a way of controlling download speed etc Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/#findComment-1151270 Share on other sites More sharing options...
noXstyle Posted December 25, 2010 Share Posted December 25, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/#findComment-1151275 Share on other sites More sharing options...
laffin Posted December 25, 2010 Share Posted December 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/#findComment-1151283 Share on other sites More sharing options...
marklarah Posted December 25, 2010 Author Share Posted December 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/#findComment-1151326 Share on other sites More sharing options...
.josh Posted December 25, 2010 Share Posted December 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222552-how-bad-is-fread-etc/#findComment-1151385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.