Graxeon Posted December 3, 2009 Share Posted December 3, 2009 I have 2 clients on 2 different servers/hosts. Client 1 has "file.ext" and Client 2 needs to load that file. The location of file.ext is encrypted so when Client 2 tries to access the file his browser doesn't force a "File Save As..." action so only a blank page comes up. This code forces the "File Save As..." action but it uses up Client 2's bandwidth. Client 2 is already paying for the file so we can't use his bandwidth. Client 1 is the one whose bandwidth needs to be used: <?php $file_url = "http://www.site.com/#####/file.ext/"; header("Content-Type: afile/ext"); header("Content-Disposition: attachment; filename=final.ext;" ); header("Content-Length: ".$size); //$size is part of the rest of the code. It's just a number like 112890. readfile($file_url); ?> How can I alter that code so that Client 1's bandwidth is used and not Client 2's (the guy that's downloading the file). Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 Unless you just redirect to that url then you can't make it look like it's coming from your server without it taking bandwidth The only way I can see you doing it is with a header('Location: '.$file_url); instead of the other header calls and the readfile line Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 3, 2009 Author Share Posted December 3, 2009 Well...see the code that I gave uses up Client 2's bandwidth even the file itself is located on Client 1's server. So I'm trying to reverse that. The code you provided doesn't work because, again, the actual location of "file.ext" is encrypted and the location changes every time it is accessed. So, just redirecting to $file_url gives a BLANK page. I'm trying to force a "File Save As..." on that redirect link while using Client 1's bandwidth (like it should). Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 3, 2009 Author Share Posted December 3, 2009 Anyone know? Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2009 Share Posted December 3, 2009 The truth of the matter is your most likely using both client's bandwidth. Most network providers monitor both directions. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 It will never work then. You are essentially proxying, so the download will be coming from server2 as you are downloading from server1 and outputting the data (which is the download from server1) Quote Link to comment Share on other sites More sharing options...
phpretard Posted December 3, 2009 Share Posted December 3, 2009 I use $_GET <?php $Type = $_GET['source']; $id = $_GET['id']; $track = $_GET['track']; $doc=$_GET['doc']; $file_path = $_SERVER['DOCUMENT_ROOT']."/orders/".$track."/".$doc; if (file_exists($file_path) && !is_dir($file_path)) { header('Content-type: application/octet-stream'); header("Content-Disposition: attachment; filename=\"".$doc."\""); header('Content-Length: ' . filesize($file_path)); ob_clean(); flush(); readfile($file_path); exit; } else { header ("HTTP/1.0 404 Not Found"); exit; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2009 Share Posted December 3, 2009 I use $_GET <?php $Type = $_GET['source']; $id = $_GET['id']; $track = $_GET['track']; $doc=$_GET['doc']; $file_path = $_SERVER['DOCUMENT_ROOT']."/orders/".$track."/".$doc; if (file_exists($file_path) && !is_dir($file_path)) { header('Content-type: application/octet-stream'); header("Content-Disposition: attachment; filename=\"".$doc."\""); header('Content-Length: ' . filesize($file_path)); ob_clean(); flush(); readfile($file_path); exit; } else { header ("HTTP/1.0 404 Not Found"); exit; } ?> So helpful. Quote Link to comment Share on other sites More sharing options...
phpretard Posted December 3, 2009 Share Posted December 3, 2009 "So Helpful" I don't know if you are kidding. This is what I use to force a download though. Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2009 Share Posted December 3, 2009 "So Helpful" I don't know if you are kidding. This is what I use to force a download though. Did you read the actual post? Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 3, 2009 Author Share Posted December 3, 2009 @phpretard The code you gave does force a download. However, the downloaded file is 0 bytes so it's not downloading the real file. Also, even that 0 byte file is coming from Client 2's website so it's using his bandwidth. So even if that downloaded the correct file, apparently it's still using the wrong bandwidth. Oh and also: Your actual script didn't work as you put it. I had to take out the "if" statement to get the 0 byte download going. I don't know what $_SERVER['DOCUMENT_ROOT'] is, though. @Jay Why won't it work? Not sure why it would be a proxy. The code should work on whatever server it's on. Heck...it's going to be on Client 2's server so there is no third server involved Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 3, 2009 Author Share Posted December 3, 2009 From how I see it...apparently "readfile($file_url)" is causing the bandwidth usage to come from BOTH Client 1 and Client 2. What other way can I "readfile" on $file_url with the "header('Content" tags attached? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 From what you've said, you would only think there are two servers but by the looks of it you have a third computer which calls the script. Can you tell me if the following is the right process... You call the script xyz.php on server 2 (which is the script in your first post) This script then reads the file from server 1 via the readfile and outputs it You then get the save as dialog on your computer Is that the correct way it's done? Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 3, 2009 Author Share Posted December 3, 2009 xyz.php is on Server 2 Client 2's server Server 1 has file.ext xyz.php calls the file that's on Client 1's server xyz.php is part of different feature on Client 2's site so xyz.php is called whenever someone access Client 2's feature. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted December 3, 2009 Share Posted December 3, 2009 Yes but whenever that script is called it produces output the equivalent of the original file size, and since that gets transferred to whatever calls the script in the first place, it uses that much bandwidth Likie I said earlier, it's essentially proxying Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 3, 2009 Share Posted December 3, 2009 What you are trying to accomplish is plain simply not possible. Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 4, 2009 Author Share Posted December 4, 2009 Then why is it that if you were to redirect to a 3mb image it only uses up your bandwidth for running the script and NOT for loading the 3mb image? Like so: <?php header('Location: http://server1.com/some3mbimage.jpg'); ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 4, 2009 Share Posted December 4, 2009 Can you see the difference between these two things? In either case, A wants to get a book and in either case he asks you. Scenario 1: You tell A to go to the library and A drives from his home to the library to get it. (the book has to travel between two places) Scenario 2: You go to the library to get the book. Then you tell A he can pick up the book at your house. (the book has to travel between three places) Now consider this: A wants to get a file, he asks your server (B) for it. Your server knows where it is (on server C). Scenario 1: Your server tells A the URI for the file on C. A downloads it from C. (the file has to travel between two computers) Scenario 2: Your server downloads the file from B. Your server then allows A to download it from your server. A downloads it from B. (the file has to travel between three computers) Quote Link to comment Share on other sites More sharing options...
trq Posted December 4, 2009 Share Posted December 4, 2009 Then why is it that if you were to redirect to a 3mb image it only uses up your bandwidth for running the script and NOT for loading the 3mb image? Like so: <?php header('Location: http://server1.com/some3mbimage.jpg'); ?> Because you are directing your clients to make another request from another completely different server than yours. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 4, 2009 Share Posted December 4, 2009 xyz.php is on Server 2 Client 2's server Server 1 has file.ext xyz.php calls the file that's on Client 1's server xyz.php is part of different feature on Client 2's site so xyz.php is called whenever someone access Client 2's feature. Yes. Unless you use other means to redirect the user to download from client 1, than you will be transfering the data from client 1 via client 2. Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 4, 2009 Author Share Posted December 4, 2009 Then why is it that if you were to redirect to a 3mb image it only uses up your bandwidth for running the script and NOT for loading the 3mb image? Like so: <?php header('Location: http://server1.com/some3mbimage.jpg'); ?> Because you are directing your clients to make another request from another completely different server than yours. Ok, so how do I change my code to do what you just said? A plain old redirect doesn't work because apparently the browser or whatever is not recognizing it as a downloadable file. How do I redirect to that URL with the instructions that that file IS a downloadable file based on your statement? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 4, 2009 Share Posted December 4, 2009 Well, you are always downloading the file. The browser is free to do whatever it wants with the data you send it. An image cannot be shown to the user without having downloaded it and an HTML page cannot be parsed without having downloaded it. Quote Link to comment Share on other sites More sharing options...
Graxeon Posted December 4, 2009 Author Share Posted December 4, 2009 *sigh* alright... Oh well, thanks guys. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 5, 2009 Share Posted December 5, 2009 check out example #1 http://us.php.net/manual/en/function.header.php Edit: Never mind... Quote Link to comment 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.