?
Solved

PHP download

Posted on 2003-03-28
46
Medium Priority
?
403 Views
Last Modified: 2008-03-17
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
Comment
Question by:lolPHP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 23
  • 11
  • 9
  • +1
46 Comments
 
LVL 1

Expert Comment

by:nbthanh
ID: 8225206
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
 

Expert Comment

by:teekai
ID: 8226406
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
 
LVL 15

Expert Comment

by:VGR
ID: 8226724
use force-download, it's wonderful
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Expert Comment

by:teekai
ID: 8229807
Is it okay?
0
 

Author Comment

by:lolPHP
ID: 8230483
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
 
LVL 15

Expert Comment

by:VGR
ID: 8230589
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
 

Expert Comment

by:teekai
ID: 8230738
Cool script!
0
 

Author Comment

by:lolPHP
ID: 8230749
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
 

Author Comment

by:lolPHP
ID: 8230753
also do you know places that offer free php hosting..?
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8230777
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
 

Author Comment

by:lolPHP
ID: 8231133
you guys are great! thank you so much.... i'm trying to find the right host now...
0
 
LVL 15

Expert Comment

by:VGR
ID: 8231255
"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
 

Author Comment

by:lolPHP
ID: 8233151
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
 

Author Comment

by:lolPHP
ID: 8233158
by the way tere are no adds on portland
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8233165
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
 
LVL 15

Expert Comment

by:VGR
ID: 8233200
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
 

Author Comment

by:lolPHP
ID: 8233236
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
 

Author Comment

by:lolPHP
ID: 8233239
notice the bit where i said bearly... sorry typing error the "b" and "n" are very close to each other on the keyboard!
0
 

Author Comment

by:lolPHP
ID: 8233246
notice the bit where i said bearly... sorry typing error the "b" and "n" are very close to each other on the keyboard!
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8233250
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
 

Author Comment

by:lolPHP
ID: 8233329
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
 

Author Comment

by:lolPHP
ID: 8233352
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
 

Author Comment

by:lolPHP
ID: 8233358
why does the $stdpath variable need to be there . ?
0
 
LVL 15

Expert Comment

by:VGR
ID: 8233364
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
 
LVL 15

Expert Comment

by:VGR
ID: 8233369
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
 
LVL 1

Expert Comment

by:nbthanh
ID: 8233370
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
 
LVL 15

Expert Comment

by:VGR
ID: 8233374
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
 
LVL 15

Expert Comment

by:VGR
ID: 8233378
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
 

Author Comment

by:lolPHP
ID: 8233782
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
 
LVL 1

Expert Comment

by:nbthanh
ID: 8233881
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
 

Author Comment

by:lolPHP
ID: 8233989
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
 
LVL 15

Expert Comment

by:VGR
ID: 8234012
0
 

Author Comment

by:lolPHP
ID: 8234016
If i havebt got a server how do i make my computer PHP compatible?  Thanks guys!
0
 

Author Comment

by:lolPHP
ID: 8234027
nah, that doesnt work either. It just comes with the error message!
0
 

Author Comment

by:lolPHP
ID: 8234052
nah, that doesnt work either. It just comes with the error message!
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8234125
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
 

Author Comment

by:lolPHP
ID: 8240991
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
 

Author Comment

by:lolPHP
ID: 8241017
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
 
LVL 1

Expert Comment

by:nbthanh
ID: 8243199
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
 

Author Comment

by:lolPHP
ID: 8246243
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
 

Author Comment

by:lolPHP
ID: 8252522
so what do you suggest then?
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8252584
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
 

Author Comment

by:lolPHP
ID: 8261946
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
 
LVL 1

Accepted Solution

by:
nbthanh earned 240 total points
ID: 8263297
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
 

Author Comment

by:lolPHP
ID: 8275544
Thanks very much , you have continuisly helped me through the jungle of PHP!
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8275571
Hi lolPHP,
I am glad that you finally find a solution for your problem.

Have a nice weekend :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question