PHP 中用 htmlspecialchars() 对特殊字符进行编码的弊端

    当对表单传递过来的参数用 htmlspecialchars 对特殊字符(& ,' ," ,< ,> )进行编码时(由于插入数据库安全过滤的需要),会出现如下问题。

   

    如果用户上传了一个文件是带有特殊字符的,如 ' ,文件名保存到数据库就会发生以下问题。

 



 

 

 

    如果你服务器端的 PHP 代码是通过 $_GET['id'] 间接来获取它的文件名,然后以名称传输到客户端。

 

<?php    
$name = mysql_query('SELECT name FROM accessories');//通过 ID 获取文件名
//......获取文件......
$file_path = ROOT_PATH . '/uploads/accessories/'. $name;
header ( 'Content-Disposition: attachment; filename=' . urlencode ( $name ) );
header ( 'Content-type: application/octet-stream;' );
header ( 'Content-Length: ' . filesize ( $file_path ) );
readfile ( $file_path );
exit ();
?>​

 

   那么下载的时候就会出现如下文件名错误。


个人主页: https://plus.google.com/+sherlockwang/posts

原文链接:http://woqilin.blogspot.com/2012/10/php-htmlspecialchars.html

 

猜你喜欢

转载自wangzq-phper.iteye.com/blog/2295718
今日推荐