Re: [Pro. PHP Dev.] Download file from server to client machine

2,354 views
Skip to first unread message
Message has been deleted

Robert Gonzalez

unread,
Oct 15, 2011, 10:17:22 AM10/15/11
to professi...@googlegroups.com
You either need to echo the file_get_contents call or use readfile, which kinda does the same thing.

On Fri, Oct 14, 2011 at 9:58 PM, priyanka dhukte <priya....@gmail.com> wrote:

Hi all,
can anyone tell me the solution to download file from server to the local machine, i have used header function to do this, but right now only the file is creating at the destination drive but the actual data from the server file is not getting there in client machine's file,
 

$path = $_GET['download_file'];
$mm_type="application/octet-stream";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
file_get_contents($path); // outputs the content of the file
exit();
 
here in the $path i am giving the full path of the pdf which is at server,
m not getting where i am going wrong, so plese can anyone tel me where is the problem exactly.

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
 
For information or project assistance please visit :
http://www.360psg.com
 
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--

Robert Gonzalez

blitzed

unread,
Oct 15, 2011, 12:56:36 PM10/15/11
to Professional PHP Developers
hi priyanka, perhaps your server needs to be nudged into handling the
correct mime type?

I've encountered such server quirks before, for example a MS Excel
spreadsheet would not download, so had to force the set mime type on
Apache...it might be as simple as adding this to your .htaccess
file:
AddType application/pdf .pdf

let's see, ah here are examples of forcing PDF:
http://www.thingy-ma-jig.co.uk/blog/06-08-2007/force-a-pdf-to-download

goodluck!

vishal bhandare

unread,
Oct 16, 2011, 6:01:07 AM10/16/11
to professi...@googlegroups.com
Try below

<?php
/*
Make sure is your file readable
*/
$path = "/var/www/Ebook.pdf";
$mm_type="application/octet-stream";
$fsize = filesize($path);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Cache-Control: private",false);
header("Content-Type: application/pdf");
header("Content-Length: " .$fsize);

header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
/*
require to flush new line or extra space while we are reading file otherwise pdf file may get corrupt
*/
ob_clean();
flush();
/*
 Use for large file
*/
$handle = fopen("$path", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
print $contents;
fclose($handle);
exit();?>



--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--


Message has been deleted
Message has been deleted

sridhar mantha

unread,
Oct 15, 2011, 1:14:19 AM10/15/11
to professi...@googlegroups.com
Hi priyanka,

    Try readfile($path) instead of file_get_contents($path)

neeraj

unread,
Oct 15, 2011, 3:31:00 PM10/15/11
to Professional PHP Developers
ok here is the solution change the last two lines of your code to

$filedata = file_get_contents($path); // outputs the content of the
file
exit($filedata);

vishal bhandare

unread,
Oct 17, 2011, 12:26:24 PM10/17/11
to professi...@googlegroups.com
Hi priyanka,

Have you tried my code?
It is tested and it works. You should at least try it.

<?php
/*
Make sure is your file readable
*/
$path = "/var/www/Ebook.pdf";
$mm_type="application/octet-
stream";
$fsize = filesize($path);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Cache-Control: private",false);
header("Content-Type: application/pdf");
header("Content-Length: " .$fsize);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
/*
require to flush new line or extra space while we are reading file otherwise pdf file may get corrupt
*/
ob_clean();
flush();
/*
 Use for large file
*/
$handle = fopen("$path", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
print $contents;
fclose($handle);
exit();?>
--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--


Message has been deleted

vishal bhandare

unread,
Oct 20, 2011, 1:04:03 PM10/20/11
to professi...@googlegroups.com
it surely means file that you are downloading don't have proper permission to read. Please check the permission of file.

On Wed, Oct 19, 2011 at 10:22 AM, priyanka dhukte <priya....@gmail.com> wrote:
hie vishal, look i have tried this, by this only file is created at destination bt not the conents are copying there, and if i try to open that file then it gave error like file may get corrupted... 
 
$path = $_GET['download_file'];
$mm_type="application/octet-stream";
$fsize = filesize($path);
--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.
 
For information or project assistance please visit :
http://www.360psg.com
 
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--


Arun Kumar

unread,
Oct 30, 2011, 11:50:36 AM10/30/11
to professi...@googlegroups.com
Hai priyanka

I Hope This script will help you!

<?php
$file_name = $_GET['file'];
if(is_file($file_name))
{
  if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off');  }
  // get the file mime type using the file extension
  switch(strtolower(substr(strrchr($file_name,'.'),1)))
  {
    case 'pdf': $mime = 'application/pdf'; break;
    case 'zip': $mime = 'application/zip'; break;
    case 'jpeg':
    case 'jpg': $mime = 'image/jpg'; break;
    default: $mime = 'application/force-download';
  }
  header('Pragma: public');   // required
  header('Expires: 0');    // no cache

  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
  header('Cache-Control: private',false);
  header('Content-Type: '.$mime);
  header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
  header('Content-Transfer-Encoding: binary');
  header('Content-Length: '.filesize($file_name));  // provide file size
  header('Connection: close');
  readfile($file_name);    // push it out
  exit();
}
?>


Thank you

Arunkumar.T
Jr Software Engineer
Infopark Thrissur
Message has been deleted

ayman elgohary

unread,
Nov 1, 2011, 7:33:51 PM11/1/11
to professi...@googlegroups.com
this is awesome, thank you

Message has been deleted

Sam Doyle

unread,
Nov 11, 2011, 3:59:48 AM11/11/11
to professi...@googlegroups.com
You want the window.open method

______________
Sam Doyle
@sam_doyle

On 11 Nov 2011, at 07:31, priyanka dhukte <priya....@gmail.com> wrote:

Can u tell me any option to cal new window without clicking on any link,
window.location.href = "download.php?download_file='.$link.'";
its not working.
previously when i was clicking on link and then having that save as window but now i want to handle it internaly, here i want to cal download.php file where i have writen code to download file on local machine. i tried it by using header("Location:download.php");
but its not working properly, n even i tried by using window.location.href = "download.php?download_file='.$link.'"; this is also not working properly, so anyone can tel me the solution for this?
Reply all
Reply to author
Forward
Message has been deleted
0 new messages