Jump to content

Force a "File Save As"?


Graxeon

Recommended Posts

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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 :P

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

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.