简体   繁体   中英

How to download a file through my server to the client? PHP

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!

<?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.

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

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM