• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

PHP download

I’ trying to make a script that will allow me to download a file which will be seen by any a proxy server as a .tmp file when the file that is being donwloaded could be any extension. ie. The file is called file.exe but the computer sees it as file.tmp. This basically means renaming file before the actual donwload…. Please ask me any questions if you are unsure what I mean!  
0
lolPHP
Asked:
lolPHP
  • 23
  • 11
  • 9
  • +1
1 Solution
 
nbthanhCommented:
Hi lolPHP,
You can do it this way:

- make a like to the file like this: download.php?filename=abc.exe&my_wanted_filename=123.tmp
- in your download.php:
<?php
    //just for sure
    header("Content-Disposition: attachment; filename=123.tmp");

    header("Content-type: application/octet-stream");

    //read content of abc.exe
    //and write to stdout
?>

Hope this help.
0
 
teekaiCommented:
header("Content-Type: application/force-download\n");
header("Content-Disposition: filename=".filename."\n");
header("Content-transfer-encoding: ascii\n");
header("Content-length: ".filesize("path/to/filename")."\n");

//send file contents
$fp=fopen("path/to/filename", "r");
fpassthru($fp);
fclose($fp);
exit; // Anti save for all document from this file ;)

Here, I tested it! It's working fine!
0
 
VGRCommented:
use force-download, it's wonderful
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
teekaiCommented:
Is it okay?
0
 
lolPHPAuthor Commented:
umm.. thanks very much guys for the code .. um... teekai the code is such that ts downloads the actual download.php file that i make what things in the script do i have to change .... for making it donwload the file i want and where would be good place to learn php?... thanks again... :)
0
 
VGRCommented:
follow nbthanh's advice combined with some details of teekai, it does what you want :

assuming you call the script this way :
download.php?filename=abc.exe&my_wanted_filename=123.tmp

<?php
  // set this constant
  $stdpath='./';
  //
  $filepath=$stdpath.$_GET['filename'];
  $wantedname=$stdpath.$_GET['my_wanted_filename'];
   //just for sure
   header("Content-Disposition: attachment; filename=$wantedname");
   header("Content-type: application/force-download");
   //read content of abc.exe
   //and write to stdout
  $fp=fopen("$filepath", "r"); // or "rb"
  fpassthru($fp); // output goes to stdout
  fclose($fp);
?>

0
 
teekaiCommented:
Cool script!
0
 
lolPHPAuthor Commented:
here is were i have put the .php file.

http://phptest5.netfirms.com/cgi-bin/download.php?filename=abc.exe&my_wanted_filename=123.tmp


i have cahnged the abc.exe to say (for example)
http://www.hello.com/hello.exe
 So i have used this code EXACTLY:


<html>
<head>
</head>
<body>

<?php
// set this constant
$stdpath='./';
//
$filepath=$stdpath.$_GET['filename'];
$wantedname=$stdpath.$_GET['my_wanted_filename'];
//just for sure
header("Content-Disposition: attachment; filename=$wantedname");
header("Content-type: application/force-download");
//read content of abc.exe
//and write to stdout
$fp=fopen("$filepath", "r"); // or "rb"
fpassthru($fp); // output goes to stdout
fclose($fp);
?>
</body>
<html>

----------code ends here------
it still doesnt work... i dont know what to do.. please go through what you do... thanks again ..guys!
0
 
lolPHPAuthor Commented:
also do you know places that offer free php hosting..?
0
 
nbthanhCommented:
Hi lolPHP,
When you send headers to clients, be sure *absolutly* nothing was output berfore, even a single space character!
Netfirms surely will put their ads code into your script, hence your header() calls won't work!

There are plenty free hostings, and free PHP hostings as well. However, there are not many ones offer you a *non-ads* free hostings!

Try:
- http://www.free-php-hosting.com/
- http://www.oinko.net/freephp/
- http://www.0php.com/free_PHP_hosting.php

And of course :-)
http://www.google.com/search?sourceid=navclient&ie=UTF-8&oe=UTF-8&q=free+php+hosting

Have a nice weekend :-)
0
 
lolPHPAuthor Commented:
you guys are great! thank you so much.... i'm trying to find the right host now...
0
 
VGRCommented:
"http://www.hello.com/hello.exe" ???

you said you wanted to pass
http://www.hello.com/index.php?filename=hello.exe

if not, the it's the PATH_INFO or the QUERY_STRING that we have to parse for parameters. No big stuff but it really depends on the server's settings
0
 
lolPHPAuthor Commented:
http://phptest5.port5.com/download.php?filename=abc.exe&my_wanted_filename=123.tmp

there is a host who will put no adds on my website..

but ... suprise .. suprise.. im doing something very stupid.. probably....


<html>
<head>
</head>
<body>

<?php
header("Content-Disposition: attachment; filename=$wantedname");
header("Content-type: application/force-download");
$stdpath='./';
$filepath=$stdpath.$_GET['filename'];
$wantedname=$stdpath.$_GET['my_wanted_filename'];
//just for sure
$fp=fopen("$filepath", "r"); // or "rb"
fpassthru($fp); // output goes to stdout
fclose($fp);
?>
</body>
<html>


there is my code
here is the error that i get

Warning: Cannot add header information - headers already sent by (output started at /host/p/h/p/p/o/r/phptest5.port5.com/download.php:6) in /host/p/h/p/p/o/r/phptest5.port5.com/download.php on line 7

Warning: Cannot add header information - headers already sent by (output started at /host/p/h/p/p/o/r/phptest5.port5.com/download.php:6) in /host/p/h/p/p/o/r/phptest5.port5.com/download.php on line 8
À .€..Àcgi-binÀ_download.php
Warning: 1 is not a valid File-Handle resource in /host/p/h/p/p/o/r/phptest5.port5.com/download.php on line 15









this is the code that i pass into it...

http://phptest5.port5.com/download.php?filename=http://download.com.com/redir?pid=10193711&merid=73316&mfgid=73316&edId=3&siteId=4&oId=3002-20-10193711&ontId=20<ype=dl_dlnow&lop=link&destUrl=http%3A%2F%2Fwww.divxity.com%2FGDiVX1.9.9.5.exe&my_wanted_filename=123.tmp

what am i doing wrong???
0
 
lolPHPAuthor Commented:
by the way tere are no adds on portland
0
 
nbthanhCommented:
Hi lolPHP,
Before the header() calls, you *must not* output anything. <html> is considerred as...something :-) SO you must not output these html tags.

Hence, your download.php should be like this:

<?php
header("Content-Disposition: attachment; filename=$wantedname");
header("Content-type: application/force-download");
$stdpath='./';
$filepath=$stdpath.$_GET['filename'];
$wantedname=$stdpath.$_GET['my_wanted_filename'];
//just for sure
$fp=fopen("$filepath", "r"); // or "rb"
fpassthru($fp); // output goes to stdout
fclose($fp);
?>


(note: Note other html code here)

Hope this help!
0
 
VGRCommented:
you're not following the code I posted...

No HTML output (not even a single space) before header()... unless you use ob_* functions

follow nbthanh's script, modified from mine, modified from his ;-)
0
 
lolPHPAuthor Commented:
nearly.... i have well... WEVE ...oh.. ok... YOU have bealry done it! It now downloads it self only. The error it gives is :

<b>Warning</b>:  1 is not a valid File-Handle resource in <b>/host/p/h/p/p/o/r/phptest5.port5.com/download.php</b> on line <b>10

i really dont have a clue what it is  and  what it means !

i have used JUST the code nothing else ie. <tags>
execpt php of course!

thanks

0
 
lolPHPAuthor Commented:
notice the bit where i said bearly... sorry typing error the "b" and "n" are very close to each other on the keyboard!
0
 
lolPHPAuthor Commented:
notice the bit where i said bearly... sorry typing error the "b" and "n" are very close to each other on the keyboard!
0
 
nbthanhCommented:
Hi,

Is this your line number 10: fclose($fp);
or: fpassthru($fp);?

Check if you have open the file correctly!

If the error is on fclose() line, try not to use that line by delete it or comment it out! It's because when you use fpassthru(), your file will be close automatically (see: http://www.php.net/manual/en/function.fpassthru.php), no need to call fclose()

Hope this help
0
 
lolPHPAuthor Commented:
thanks for that i deleted the fpclose!

the php now more or less works. It forces the download but again download its self. When you get the page and see the errors only this " À
.             €..            Àcgi-bin       À_download.php  
"
comes up. I dont have a clue. I pass in the address and then the rename and it doesnt work!
0
 
lolPHPAuthor Commented:
thanks for that i deleted the fpclose!

the php now more or less works. It forces the download but again download its self. When you get the page and see the errors only this " À
.             €..            Àcgi-bin       À_download.php  
"
comes up. I dont have a clue. I pass in the address and then the rename and it doesnt work!
0
 
lolPHPAuthor Commented:
why does the $stdpath variable need to be there . ?
0
 
VGRCommented:
it doesn't NEED to be there : it's just more practical to change the subdirectory you write the files to or access the files from (in your case)
0
 
VGRCommented:
ok. Basically, I used passthru() just to build upon the other script.

My solution would involve just reading and echoing the data, like this :


<?php
 // set this constant
 $stdpath='./';
 //
 $filepath=$stdpath.$_GET['filename'];
 $wantedname=$stdpath.$_GET['my_wanted_filename'];
  //just for sure
  header("Content-Disposition: attachment; filename=$wantedname");
  header("Content-type: application/force-download");
  //read content of abc.exe
  //and write to stdout
  $fp = @fopen($filepath,"rb");
  if ($fp) { // filed opened OK
      $cont='';
      set_magic_quotes_runtime(0);
      while(!feof($fp)) $cont.= fread($fp,8192);
      fclose($fp);
      echo $cont; // sends data back to client's browser
      //set_magic_quotes_runtime(get_magic_quotes_gpc());
  } // else NOP, file was not opened
?>


0
 
nbthanhCommented:
Hi lolPHP,
There are 2 problems with your code:

* First problem:
header("Content-Disposition: attachment; filename=$wantedname");
...
$wantedname=$stdpath.$_GET['my_wanted_filename'];

<== it should be in the reversed order:
first:
  $wantedname=$_GET['my_wanted_filename'];
and then:
  header("Content-Disposition: attachment; filename=$wantedname");
(note: no $stdpath here)

* Second problem:
$filepath=$stdpath.$_GET['filename'];
$fp=fopen("$filepath", "r");
<== please try not to download a remote from another site since your server might disable this feature! You let filename=http://....In this case, do NOT put $stdpath there!
Is should be like this:
  $filepath=$_GET['filename'];
  $fp=fopen("$filepath", "rb");

Try this first:
download.php?filename=download.php&my_wanted_name=abc.bin/abc.bin

(the /abc.bin at the end is just for sure that file abc.bin will be saved. IF your server and/or browser doesnot support this feature, please remore the /abc.bin)

And see if you can download the download.php's source code itself.

Hope this help.
0
 
VGRCommented:
I would even suggest this : (that's the way I would have written it myself from scratch)

<?php
// set this constant
$stdpath='./';
//
$filepath=$stdpath.$_GET['filename'];
$wantedname=$stdpath.$_GET['my_wanted_filename'];
//read content of abc.exe
//and write to stdout
$fp = @fopen($filepath,"rb");
if ($fp) { // filed opened OK
  header("Content-Disposition: attachment; filename=$wantedname");
  header("Content-type: application/force-download");
  $cont='';
  set_magic_quotes_runtime(0);
  while(!feof($fp)) $cont.= fread($fp,8192);
  fclose($fp);
  echo $cont; // sends data back to client's browser
  //set_magic_quotes_runtime(get_magic_quotes_gpc());
} else echo "Sorry, error opening file '$filepath'<BR";
?>


0
 
VGRCommented:
and as for the above comment, yes, true, but the script is supposed to serve DOWNLOADS, so LOCAL files IMHO. Files taken from $stdpath. That's all.
0
 
lolPHPAuthor Commented:
right... this has gon on too long.. i'm sure you guys are tired of me by now!!


NBTHANH: I have tried what you told me to do. I have taken away the extra bit in  front of the address and alos put the hearder later .. after the get command. It doesnt ork. It just downloads itself . I put in /download.php?filename=http://www.exetools.com/files/compressors/upx/upx121d.zip&my_wanted_filename=123.tmp. It just wont work. I =t gives me that garbage that i showed you earlier.
here is the link
http://phptest5.port5.com/download.php?filename=http://www.exetools.com/files/compressors/upx/upx121d.zip&my_wanted_filename=yo.tmp

VGR: i have also used ur script . It does exactly the same as nbthanh's.

Maybe prtland are not a very good server or something.

Why dont you guys try it and see if it works. Just to recap i am trying to rename a file before downloading it .

Thanks very much guys. Ill see you later.

LOLPHp
0
 
nbthanhCommented:
Hi lolPHP,
Try this one:

<?php
     $file_to_read = $_GET['filename'];
     $file_to_save = $_GET['wanted_filename'];

     @ $fp = fopen($file_to_read,"rb");

     if (!$fp) {
          echo "Error: $file_to_read not found!";
     } else {
          header("Content-Disposition: attachment; filename=$file_to_save");
          header("Content-type: application/octet-stream");
          fpassthru($fp);
     }
?>

I tried on my PC and it did work well!

Here what I tested:

http://localhost/download.php?filename=download.php&wanted_filename=123.bin
==> worked!

http://localhost/download.php?filename=/etc/passwd&wanted_filename=password.txt
==> worked!

lolPHP please do NOT try this:
http://yourdomain/download.php?filename=http://another_domain/secret_file.bin&wanted_filename=i_wanna_get_it.txt

Because it will download the http://another_domain/secret_file.bin on *another* server and this will mostly failed!

You may wish to test the script with cases that you think it would not be failed:
download.php?filename=download.php&wanted_filename=hello.txt
==> download the file download.php itself and save to hello.txt

download.php?filename=/etc/passwd&wanted_filename=i_got_the_passwords.txt
==> download the file /etc/passwd (this file is on your server and usually readable) save to i_got_the_passwords.txt

Hope this help
0
 
lolPHPAuthor Commented:
The script keeps giving out Error: Not Found.

This means that the filename has not passed into the .php

I tell it to get download.php in the same folder as this script!

But it doesnot and gives the rror
0
 
VGRCommented:
0
 
lolPHPAuthor Commented:
If i havebt got a server how do i make my computer PHP compatible?  Thanks guys!
0
 
lolPHPAuthor Commented:
nah, that doesnt work either. It just comes with the error message!
0
 
lolPHPAuthor Commented:
nah, that doesnt work either. It just comes with the error message!
0
 
nbthanhCommented:
Hi lolPHP,
If $_GET['filename'] doesnot work, try $HTTP_GET_VARS['filename'] because some old versions of PHP donot support $_GET

Hope this help!
P/S:
If you want to install PHP on your computer:
- Install Apache (http://www.apache.org)
- Install PHP (http://www.php.net)
0
 
lolPHPAuthor Commented:
Right , ok it still doenst work! oh no... ok lets get this straight ill give you some facts see of you can work through them;

1) I want something that will reaname a file before it is downloaded.

2) I want to get files of other http:// websites.

3) I dont have a server.

4) I am really apriciatging your help, thanks!

lolPHP
0
 
lolPHPAuthor Commented:
basically say i am at a placeA. My .php file is at portland. At placeA when i want a file, the file is chagned to a name from say 123.tmp to 123.mp3. The file doesnt need to be written to the server but simply windows will think it is download a  file with anextension that it is not actually downloading... thanks
0
 
nbthanhCommented:
Hi lolPHP,
Try I code above on your own PC (I tested it very well with Apache & PHP 4.2+). Maybe some free hosting will mess up your code!

Hope this help.
0
 
lolPHPAuthor Commented:
I really dont have a server. The thing i want to be able to access this script from a remote area that is why i dont want to put it on the home server.
0
 
lolPHPAuthor Commented:
so what do you suggest then?
0
 
nbthanhCommented:
Hi,
You can just simply install Apache & PHP on your PC then you have a 'mini-server' for testing :-)
Apache & PHP are totally free and very light. They won't 'eat' most resource from your PC :-)

If you really want to go online the be sure to choose a hosting that won't put ads into your code.

Hope this help.
0
 
lolPHPAuthor Commented:
Well say if i have a host ie... portland then why cant this system work on there server.

remember i dont actually want to save the file
and i want a file on another domain.
0
 
nbthanhCommented:
If you really want to get file from another domain (yeah, why you do this since user can directly click on the link to get file from the other domain?) you can first download it to your server, save to a file and provide a link for your users to download from your server.
To get a file from a server, just fopen('http://domain/file.ext') and write down to your folder on your server.

Some (free) hosts will not allow you to use some file-related function (eg open file from another server). That's the reason why your code was not working.

Hope this help.
0
 
lolPHPAuthor Commented:
Thanks very much , you have continuisly helped me through the jungle of PHP!
0
 
nbthanhCommented:
Hi lolPHP,
I am glad that you finally find a solution for your problem.

Have a nice weekend :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 23
  • 11
  • 9
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now