Ik wil dat een bestand wordt gedownload als de gebruiker een webpagina met PHP bezoekt. Ik denk dat het iets te maken heeft met file_get_contents
, maar ik weet niet zeker hoe ik het moet uitvoeren.
$url = "http://example.com/go.exe";
Na het downloaden van een bestand met header(location)
wordt er niet doorverwezen naar een andere pagina. Hij stopt gewoon.
Lees de docs over de ingebouwde PHP-functie readfile
$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url); // do the double-download-dance (dirty but worky)
Zorg er ook voor dat je het juiste inhoudstype toevoegt gebaseerd op je bestand applicatie/zip, applicatie/pdf enz. - maar alleen als je het save-as dialoog niet wilt activeren.
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"file.exe\"");
echo readfile($url);
is correct
of beter één voor exe type bestanden
header("Location: $url");
<?php
$file = "http://example.com/go.exe";
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"". basename($file) ."\"");
readfile ($file);
exit();
?>
Of, als het bestand niet te openen is met de browser, kun je gewoon de Location
header gebruiken:
<?php header("Location: http://example.com/go.exe"); ?>