搜索
首页后端开发php教程这样为什么不能实现文件下载

$root="D:/wamp/www/新建文件夹/";
if(is_dir($root))
{
   $openHandle=opendir($root);
   while(false!==($file=readdir($openHandle)))
   {
   if(!is_dir($root.$file))
   {
   echo "".$file."
";
    echo "$root$file
";
   }
   }
   closedir($openHandle);
}
else 
{
echo "文件夹不存在";
}
?>
这段代码为什么不能实现文件下载 浏览器:搜狗浏览器 IE 都试了


回复讨论(解决方案)

用header头下载

用header头下载 能不能给个测试程序啊

已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}

已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}
不行啊  直接把php文件下下来了

已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}

错误代码:27

file_put_contents('download/excel.csv',$str);
header("location:download/excel.csv"); //让电脑自动弹出下载对话框

file_put_contents('download/excel.csv',$str);
header("location:download/excel.csv"); //让电脑自动弹出下载对话框 加在哪里啊  怎样加

已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}

if($do=='down')这个判断始终没有成功  后面大括号内代码没机会执行 怎么办啊

已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}

你这条语句是不是有问题啊 “echo "".$root.$file."
";”  该怎么改啊

href='?do 改成  href='a.php?do

a.php就是你自己的那?php文件的文件名


已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}

错误代码:27 ??描述是啥?????出??你看一下


已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}
不行啊  直接把php文件下下来了
我?的??方式不分文件?型皆可下?,只要?文件有?取?限就能被下?,所以你在列出要下?的文件的?候要排除掉php?型.

我的文件名叫 download.php 改了也还是不行 你帮我看看吧 
$root="D:/wamp/www/panjinli/downloadsouce";
if(is_dir($root))
{
   $openHandle=opendir($root);
   while(false!==($file=readdir($openHandle)))
   {
       if(!is_dir($root.$file))
   {
         echo "".$root.$file."
";
       }
   }
   closedir($openHandle);
}else
{
echo "文件夹不存在";
}
 
$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';
if($do=='down')
{
    $file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';
    $path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';
 
    OB_start();
    function DownLoad($info,$file) 
{
        OB_Clean();
        if(!file_exists($info))
{
            exit('Error:'.__LINE__.','.basename(__FILE__).'!');
        }
        $info=file_get_contents($info);
 
        header('Content-Transfer-Encoding: binary');
        header('Content-Type: application/force-download; name="'.$file.'"');
        header('Content-Disposition: attachment; filename='.$file.'');
        header('Cache-control: private');
        header('Pragma: no-cache');
        header('Expires: 0');
        echo $info;
        die();
    }
    DownLoad($path.$file,$file);
}
?>




已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}
不行啊  直接把php文件下下来了
我?的??方式不分文件?型皆可下?,只要?文件有?取?限就能被下?,所以你在列出要下?的文件的?候要排除掉php?型.



已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}
不行啊  直接把php文件下下来了
我?的??方式不分文件?型皆可下?,只要?文件有?取?限就能被下?,所以你在列出要下?的文件的?候要排除掉php?型.
你是在LINUX下测的吗  我放在WINDOWS上不能成功执行  放在LINUX下载的文件会被写入一部分乱七八糟的内容  求支援啊




已??你?好了

<?php$root="D:/ghost/";if(is_dir($root)){   $openHandle=opendir($root);   while(false!==($file=readdir($openHandle))){	   if(!is_dir($root.$file)){		 echo "<a href='?do=down&file=".$file."&path=".urlencode($root)."'>".$root.$file."</a><br/>";	   }   }   closedir($openHandle);}else{echo "文件夹不存在";}$do=(isset($_REQUEST['do'])) ? addslashes($_REQUEST['do']) : '';if($do=='down') {	$file=(isset($_REQUEST['file'])) ? addslashes($_REQUEST['file']) : '';	$path=(isset($_REQUEST['path'])) ? addslashes($_REQUEST['path']) : '';	OB_start();	function DownLoad($info,$file) {		OB_Clean();		if(!file_exists($info)) {			exit('Error:'.__LINE__.','.basename(__FILE__).'!');		}		$info=file_get_contents($info);		header('Content-Transfer-Encoding: binary');		header('Content-Type: application/force-download; name="'.$file.'"');		header('Content-Disposition: attachment; filename='.$file.'');		header('Cache-control: private');		header('Pragma: no-cache');		header('Expires: 0');		echo $info;		die();	}	DownLoad($path.$file,$file);}
不行啊  直接把php文件下下来了
我?的??方式不分文件?型皆可下?,只要?文件有?取?限就能被下?,所以你在列出要下?的文件的?候要排除掉php?型.
你是在LINUX下测的吗  我放在WINDOWS上不能成功执行  放在LINUX下载的文件会被写入一部分乱七八糟的内容  求支援啊 我在windows和linux下都???,一切正常

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
使用数据库存储会话的优点是什么?使用数据库存储会话的优点是什么?Apr 24, 2025 am 12:16 AM

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

您如何在PHP中实现自定义会话处理?您如何在PHP中实现自定义会话处理?Apr 24, 2025 am 12:16 AM

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

什么是会话ID?什么是会话ID?Apr 24, 2025 am 12:13 AM

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

您如何在无状态环境(例如API)中处理会议?您如何在无状态环境(例如API)中处理会议?Apr 24, 2025 am 12:12 AM

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

您如何防止与会议有关的跨站点脚本(XSS)攻击?您如何防止与会议有关的跨站点脚本(XSS)攻击?Apr 23, 2025 am 12:16 AM

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

您如何优化PHP会话性能?您如何优化PHP会话性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

什么是session.gc_maxlifetime配置设置?什么是session.gc_maxlifetime配置设置?Apr 23, 2025 am 12:10 AM

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

您如何在PHP中配置会话名?您如何在PHP中配置会话名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

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

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境