Connecting Tech Pros Worldwide Forums | Help | Site Map

file download script

rpnew's Avatar
Familiar Sight
 
Join Date: Aug 2007
Posts: 180
#1: Aug 22 '07
Hi,

I'm very new to PHP and making one project..
what i want to do is .. i want to make a file download facilities thorough which user of my web pages can download selected file. presently i'm using one code (got from internet) i tried to modified it every way but every time file download dialog box opens but it downloads blank file.. neither it checks for file existence.....

any help would be appreciated..

Presently i'm using following code.. i'm working on FC4:php/mysql..

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $f_name='xyz.txt';
  3. $f_location='download/'.$f_name;
  4. if ( ! file_exists( $f_name ) ) 
  5. {
  6.   echo "File not found.";
  7.   exit;
  8. };
  9. if(ini_get('zlib.output_compression'))
  10.   ini_set('zlib.output_compression', 'Off');
  11. header("Pragma: public"); // required
  12. header("Expires: 0");
  13. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  14. header("Cache-Control: private",false); // required for certain browsers 
  15. header("Content-Type: PHP Generated Data");
  16. header("Content-Length: " .(filesize($f_name)) );
  17. header("Content-Disposition: attachment; filename=".basename($f_location).";");
  18. header("Content-Transfer-Encoding: binary\n");
  19. @readfile($f_name);
  20. ?>
  21.  
any enhancement in the code or a new code will be great help to me....

thanks in advance...

RP

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Aug 22 '07

re: file download script


Hi.

In line 4, you are checking if the file name exists ($f_name). I would have thought you wanted to check the full file location ($f_location)?
rpnew's Avatar
Familiar Sight
 
Join Date: Aug 2007
Posts: 180
#3: Aug 22 '07

re: file download script


well thanks for the reply...

you are right for that now it atleast checks for the file existence..... but my main problem is not that.. my problem is.. file dialog box opens and i can download a file with the name i specified in the code but every time it downloads a blank file means without any content in it like the original file has.
any suggestions for that????

RP
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#4: Aug 22 '07

re: file download script


Maybe because you did the exact same thing with the readfile() function?

Try replacing the $f_name in the readfile() function with $f_location.
Also, remove the @ sign from the function. I always find that debugging goes a lot better if I can see the errors :)
rpnew's Avatar
Familiar Sight
 
Join Date: Aug 2007
Posts: 180
#5: Aug 22 '07

re: file download script


Quote:

Originally Posted by Atli

Maybe because you did the exact same thing with the readfile() function?

Try replacing the $f_name in the readfile() function with $f_location.
Also, remove the @ sign from the function. I always find that debugging goes a lot better if I can see the errors :)

well thanks again.. but i still have the same problem.. let me give you more information....

my main page is in HTML directory.. on that page there is one hyperlink which calls another page which containg above PHP code and only that code...... now the file i want to download is in html/download directory........
(this is all for testing purpose)

?????

RP
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#6: Aug 22 '07

re: file download script


I see no reason why this wouldn't work. I have pretty much the same code in a project I'm currently working on, which works fine.

I'm not entirely sure what the zlib compression stuff is doing there, but I can't see how that would mess this up.

Unless you also forgot to replace the $f_name in the filesize() function?

This is the code I'm using. As I say, it's pretty much identical to yours but I have tested it and it works perfectly. (Running Fedora 7 using PHP 5.1.6)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $filename = "path/to/file/filname.ext";
  3.  
  4.     if(!file_exists($filename)) {
  5.         die("File does not exist!");
  6.     }
  7.  
  8.     header("Pragma: public");
  9.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  10.     header("Cache-Control: private",false);
  11.     header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
  12.     header("Content-Transfer-Encoding: binary");
  13.     header("Content-Type: PHP Generated Data");
  14.     header("Content-Length: ".filesize($filename));
  15.     readfile("$filename");
  16.     exit();
  17. ?>
  18.  
rpnew's Avatar
Familiar Sight
 
Join Date: Aug 2007
Posts: 180
#7: Aug 22 '07

re: file download script


Quote:

Originally Posted by Atli

I see no reason why this wouldn't work. I have pretty much the same code in a project I'm currently working on, which works fine.

I'm not entirely sure what the zlib compression stuff is doing there, but I can't see how that would mess this up.

Unless you also forgot to replace the $f_name in the filesize() function?

This is the code I'm using. As I say, it's pretty much identical to yours but I have tested it and it works perfectly. (Running Fedora 7 using PHP 5.1.6)

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $filename = "path/to/file/filname.ext";
  3.  
  4.     if(!file_exists($filename)) {
  5.         die("File does not exist!");
  6.     }
  7.  
  8.     header("Pragma: public");
  9.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  10.     header("Cache-Control: private",false);
  11.     header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
  12.     header("Content-Transfer-Encoding: binary");
  13.     header("Content-Type: PHP Generated Data");
  14.     header("Content-Length: ".filesize($filename));
  15.     readfile("$filename");
  16.     exit();
  17. ?>
  18.  


well...
dunno but still it dosent work... i've change my code according to yours..
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $f_name="download/xyz.txt";
  3. if (!file_exists($f_name)) 
  4. {
  5.   echo "File not found.";
  6.   exit;
  7. };
  8. header("Pragma: public"); // required
  9. header("Expires: 0");
  10. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  11. header("Cache-Control: private",false); // required for certain browsers 
  12. header("Content-Type: text");
  13. header("Content-Length: " .(filesize($f_name)).";" );
  14. header("Content-Disposition: attachment; filename=".basename($f_name).";");
  15. header("Content-Transfer-Encoding: binary\n");
  16. readfile("$f_name");
  17. exit();
  18. ?>
  19.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#8: Aug 22 '07

re: file download script


Try removing the newline char ('\n') from the last header. I know HTTP transactions rely heavily on newline characters.

Also, try removing the ; character from the end of your Content-length and Content-Disposition headers.

Finally put the file name in the Content-Disposition header inside quote marks, like I did in my code.
rpnew's Avatar
Familiar Sight
 
Join Date: Aug 2007
Posts: 180
#9: Aug 22 '07

re: file download script


well thanks a lot for your help... finaly i got the script working... the problem was with the permission..... it didnt have even read permission for other user then owner i changed them and used your code it works now..


Thanks a lot once again....


RP
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#10: Aug 22 '07

re: file download script


It's always those simple things that cause the most trouble :)

Anyways, glad you got it working.
Reply