1

There's a file on a server. The file is a ZIP file.

The file has to be downloaded through my server to the client.

I tried using the readfile function but instead of downloading a file it just displays wierd symbols...

I uploaded it here: http://b-games.co.il/test/download/

This is the code:

    $file = "2title3.gif";
if(file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}

I also tried using this:

$url = 'http://example.com/some.url';
$src = fopen($url, 'r');
$dest = fopen('php://output', 'w');
$bytesCopied = stream_copy_to_stream($src, $dest);

But it did the same thing... Just displayd wierd symbols.

What am I doing wrong? http://php.net/manual/en/function.readfile.php Here it says it should work...

An important update:

On my localhost the same code works!

Anyone knows why?

Thanks!

Dor Aharonson
  • 143
  • 1
  • 1
  • 9

4 Answers4

3
<?php 
header('Content-disposition: attachment; filename=file.gif');
header('Content-type: image/gif');
readfile('file.gif');

You can try something like this, beware about settings the right content-type.

Adirael
  • 9,398
  • 2
  • 22
  • 15
  • 3
    You can use finfo_file to get the mime-type on the fly. http://us2.php.net/manual/en/function.finfo-file.php – Adirael Jun 29 '12 at 13:09
  • Don't output anything apart for the readfile! Try it here: http://ebenum.es/downloadfile.php, it will ask you to download a file named php.png with a PHP logo. – Adirael Jun 29 '12 at 14:24
0

Already this worked ..

<?php
session_start();
$a=$_GET['a'];
$parts=explode("-",$a);
$tres=$parts[0];
$nombre=$partes[1];
$dbcodletra=substr($tres,2);
if($dbcod!=$_SESSION["username"])$boolt=0;
if($ext==1) $extl=".jpg";
if($ext==2) $extl=".jpeg";
if($ext==3) $extl=".tif";
if($ext==4) $extl=".tiff";
if($ext==5) $extl=".pdf";
if($ext==6) $extl=".doc";
if($ext==7) $extl=".docx";
if($tipoproy==1) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if($tipoproy==2) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if($tipoproy==3) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if($tipoproy==4) $dir="rute/".$dbcodletra."/".$nombre.$extl;
if (file_exists($dir) && $boolt) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($dir));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($dir));
    ob_clean();
    flush();
    readfile($dir);
    exit;
}else echo "<meta http-equiv=\"Refresh\" content=\"0;url=misdocumentos.php\">";
?>

from php-image-file-download-by-code

Community
  • 1
  • 1
Bhavin Rana
  • 1,554
  • 4
  • 20
  • 40
0

It's seem a duplication of php-force-download-extension.

The "real" problem (IMHO) is that you've already generate an output, and you have not ob_start() at the beginning of the code, so ob_clean() do nothing.

EDIT

this your PHP modify to download only the GIF

<?php
ob_start();
// put some business here 
$file = "2title3.gif";
if(file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();

readfile($file);
exit;
}

EDIT 2

I look at your example code, may be I found what you really want to do. Try a look to this example.

Using HTTP you can send only one mime type per request, so you need 2 requests: one for for the HTML (showing the example) and one for the download (pressing submit button).

Community
  • 1
  • 1
Ivan Buttinoni
  • 4,110
  • 1
  • 24
  • 44
0

This seems like a problem on your server. Switch on your PHP error reporting on your Virtual server and tell us what it say. At the moment everything that should be working inst working. If all else fails maybe try JavaScript.

Drmzindec
  • 804
  • 2
  • 15
  • 28
  • It works on my local host... How do I switch to error reporting? – Dor Aharonson Jun 29 '12 at 13:59
  • What virtual server are you using? WAMP or XAMP you can just click on your tray icon, go to PHP > PHP settings > display errors. It might show us something you arent seeing, on a virtual/localhost you can set the ideal system and online servers dont always support all of the functions. – Drmzindec Jun 29 '12 at 14:01
  • Maybe look into JavaScript... [link]http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery. Are you using cheap shared hosting? Have you checked what PHP version they are running? Make sure your host is capable of supporting what you want, i run all my web apps on Hetzner Hosting and never had problems. I tested Adirael link and it worked fine. – Drmzindec Jun 29 '12 at 14:20