php three file downloads implementation

1, a direct link to add the file
<the Button>
<a href = "http://localhost/down.zip">
download file
</ button>

Click the button to download:


2, pass parameters to find and jump to the download link
for the arguments:

<the Button>
<a href = "http://localhost?f='down'">
download file
</ button>

locate the file and challenges to the download link:

<?php

$ down = $ _GET [ 'f ']; // get the file parameter
$ filename = $ down. 'zip .'; // get the file name
$ dir = "down /"; // download directory relative to the web root directory path
$ down_host = $ _SERVER [ 'HTTP_HOST '] '/';. // current domain name


// determines if the file exists, then jump to the download path
IF {(the file_exists (the DIR __ __ '/' $ $ filename the dir)...)
Header ( 'LOCATION: HTTP: //'.$down_host.$dir.$filename );
} the else {
header ( 'the HTTP / 1.1 404 Not Found');
}

results:

File exists


file does not exist

3, head () and fread () function to output directly to a file browser
<PHP?
$ File_name = "Down";
$ file_name = "down.zip"; // download file name
$ file_dir = "./down/" ; // download file storage directory
// check whether the file exists
iF (the file_exists ($ file_name file_dir $)!.) {
header ( 'the HTTP / 1.1 404 the NOT FOUND');
} the else {
// open in read-only mode and binary file
$ file = fopen ($ file_dir $ file_name, "rb".);

// tells the browser which is a file stream format file
Header ( "Content-type: application / octet-stream");
the unit of measure // request range
Header ( "the Accept-Ranges: bytes");
// Content- length is included in the request or specify the length in bytes of data in response to
Header ( "the Accept-length:.". filesize ($ file_dir $ file_name));
// used to tell the browser, the file can be downloaded as a accessory, downloads after the file name is $ file_name value of the variable.
Header ( "Content-Disposition: attachment ; filename =". $ File_name);

// read the contents of the file and output directly to the browser
echo fread ($ File, filesize ($ file_dir $ file_name).);
Fclose ($ File);
Exit ();
}

Result: The second and the same

Guess you like

Origin www.cnblogs.com/chenduzizhong/p/10986132.html
Recommended