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!

4
  • Do you have any kind of error description? "It doesn't work" is ambigious Commented Jun 29, 2012 at 13:07
  • You said the file is a zip file, but your script points to a gif file - just pointing that out Commented Jun 29, 2012 at 13:08
  • Sorry, I was triyng the ZIP file at the beggening but then switch to the gif. Commented Jun 29, 2012 at 13:26
  • I said it just dispays wierd charecters and I gave a link... b-games.co.il/test/download Commented Jun 29, 2012 at 13:27

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You can use finfo_file to get the mime-type on the fly. us2.php.net/manual/en/function.finfo-file.php
Don't output anything apart for the readfile! Try it here: ebenum.es/downloadfile.php, it will ask you to download a file named php.png with a PHP logo.
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

Comments

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.

3 Comments

It works on my local host... How do I switch to error reporting?
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.
Maybe look into JavaScript... [link]stackoverflow.com/questions/3749231/…. 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.
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).

4 Comments

I see a lot of HTML and PHP before your GIF
Have you tried my example? If yes, why it don't perform well?
Yes. On my local server everything works. But on my actual server, it doesn't
this is an unusefull answer, because you don't say what you expected from the code, you show only a snippet of code and finally say "on my PC works".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.