Php File Download-PHP source code

Source: Internet
Author: User
Ec (2); author: heaven File Download functiondownload ($ file_dir, $ file_name) parameter description: file_dir: file directory file_name: file name {& nbsp; & nbsp; $ file_dirchop ($ file_dir); Remove unnecessary spaces in the path & nbsp; & nbs script ec (2); script

// Author: heaven
// File Download
Function download ($ file_dir, $ file_name)
// Parameter description:
// File_dir: directory of the file
// File_name: File Name
{
$ File_dir = chop ($ file_dir); // remove unnecessary spaces in the path
// Obtain the path of the object to be downloaded
If ($ file_dir! = '')
{
$ File_path = $ file_dir;
If (substr ($ file_dir, strlen ($ file_dir)-1, strlen ($ file_dir ))! = '/')
$ File_path. = '/';
$ File_path. = $ file_name;
}
Else
$ File_path = $ file_name;


// Determine whether the object to be downloaded exists
If (! File_exists ($ file_path ))
{
Echo 'Sorry, the file you want to download does not exist. ';
Return false;
}

$ File_size = filesize ($ file_path );

Header ("Content-type: application/octet-stream ");
Header ("Accept-Ranges: bytes ");
Header ("Accept-Length: $ file_size ");
Header ("Content-Disposition: attachment; filename =". $ file_name );

// Echo file_get_contents ($ file_path );

$ Fp = fopen ($ file_path, "r ");
$ Buffer_size = 1024;
$ Cur_pos = 0;

While (! Feof ($ fp) & $ file_size-$ cur_pos> $ buffer_size)
{
$ Buffer = fread ($ fp, $ buffer_size );
Echo $ buffer;
$ Cur_pos + = $ buffer_size;
}

$ Buffer = @ fread ($ fp, $ file_size-$ cur_pos );
Echo $ buffer;

Fclose ($ fp );
Return true;

}

Page Processing

$ Dir = "../main/trade_log ";
// Open a known directory, and proceed to read its contents
If (is_dir ($ dir ))
{
$ Filename = array ();
If ($ dh = opendir ($ dir ))
{
While ($ file = readdir ($ dh ))! = False)
{
If ($ file! = "." & $ File! = "..." & $ File! = ". Svn ")
{

$ Filename [] = $ file;
}
}
Closedir ($ dh );
}
}

If (isset ($ _ GET ['logname'])
{

Download ("../main/trade_log", $ _ GET ['logname']);
Exit;

}
$ Smarty-> assign ("trade_log_list", $ filename );

Note

Download ("../main/trade_log", $ _ GET ['logname']);
Exit;

Make sure to add exit

Otherwise, the display part of the downloaded file must be output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.