关于php使用header实现下载功能
test1.php关键代码,这里省略链接数据库相关代码,只有从数据库中读出相关代码
$sql_select = "SELECT * FROM `file_information` order by id DESC";
$sql_mysql = mysql_query($sql_select,$connected);
?>
ID | 缩略图 | 简介 | 路径 | 长传时间 | 操作 |
---|---|---|---|---|---|
![]() |
">下载 |
test2.php代码:(由于header("Content-Type: text/html;charset=UTF-8")给予了类型,所以其他html代码全部删除了,并且没有将test2.php中的php代码写在test1.php中,因为test1.php有有输出,会影响header()设置类型
header("Content-Type: text/html;charset=UTF-8");
if(!empty($_GET['download_id']))
{
$download_id = $_GET['download_id'];
$download_id = explode("@@@",$download_id);
$download_path = $download_id[1];
if(!is_null($download_path))
{
$filename = basename($download_path);//获取文件名
$download_file = fopen($download_path,"r");
if($download_file)
{
header("Content-Type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Accept-Length:".filesize($download_path));
header("Content-Disposition: attachment; filename=".$filename);
echo fread($download_file,filesize($download_path));
fclose($download_file);
exit;
}
else
{
echo "The file must be not exist";
}
}
}
?>
这么做其实是可以的,而且下载功能也可以执行,经过测试,在IE9,chrome,firefox上面都可以运行,jpg文件,png文件,txt文件,swf文件都可以下载并查看,但是基于chrome内核的360极速浏览器确出现问题,当图片是png格式时,可以下载并打开,当图片是jpg格式时,可以下载,大小却为0B,打不开图片,这应该是浏览器问题吧?不过chrome都可以为什么它不可以??求好银不思赐教why,thank you!(ps:360极速浏览器的极速模式,兼容模式,IE9模式均不可以下载jpg格式图片)
------解决方案--------------------
if(file_exists($download_file)){
//
}

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。