Home  >  Article  >  Backend Development  >  PHP file download example code_PHP tutorial

PHP file download example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:07:16974browse

php tutorial file download example code
function xiazai($file_dir,$file_name)
//Parameter description:
//file_dir: the directory where the file is located
//file_name: file name
{
$file_dir = chop($file_dir);//Remove extra spaces in the path
//Get the path of the file 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 file to be downloaded exists if(!file_exists ($file_path))
{
alert('Sorry, the file you want to download does not exist');
return false;
}

$file_size = filesize($file_path );

header("Content-type: application/octet-stream;charset=gbk");

header("Accept-Ranges: bytes");
header("Accept-Length : $file_size");
header("Content-Disposition: attachment; filename=".$file_name);

$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;
echo $cur_pos += $buffer_size;
}

$buffer = fread($fp,$file_size-$cur_pos); $buffer;
fclose($fp);
return true;

}


http://www.bkjia.com/PHPjc/444963.html

truehttp: //www.bkjia.com/PHPjc/444963.htmlTechArticlephp tutorial file download example code function xiazai($file_dir,$file_name) //Parameter description: //file_dir: The directory where the file is located //file_name: file name { $file_dir = chop($file_dir); //Remove the path...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn