是否可以打开.htaccess文件并取消注释PHP中的所有cols?
我正在使用PHP的imap功能并在服务器上保存电子邮件附件。现在,当用户发送包含.htaccess文件的电子邮件用于黑客攻击时,我将取消注释.htaccess文件中的每一行,然后将其保存在服务器上。
---------------------------------------------------------------------
| file_id | song_id | song_location | bitrate_id |
----------------------------------------------------------------------
| 1 | 1 | loc 1 | 1 |
----------------------------------------------------------------------
| 2 | 2 | loc 2 | 1 |
---------------------------------------------------------------------
我当前的.htaccess文件如下所示:
if ($emails[$i]['attachments'][$n]['name'] == '.htaccess') {
/* Uncomment all the lines with # */
}
因此用户无法直接访问文件,但内容会上传到特定于用户的子文件夹,因此他们可以上传.htaccess,从而覆盖定义的规则。
我不允许用户直接下载附件。他们看到生成的链接如下:
Order allow, deny
Deny from all
RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
RemoveType .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
php_flag engine off
在msgAttachmentDownload.php中,我从数据库中取出url并下载它:
$msgfile = '../php/downloads/msgAttachmentDownload.php?download=' . urlencode(base64_encode($r16['id']));
$fileLink = '<a target="_blank" href =" ' . $msgfile . '"><i class="fa fa-download fa-fw"></i> ' .$r16['filename']. ' (' . $msgfilesize . ' MB)</a>';
因此,如果我是对的,用户永远不会看到服务器上文件的真实路径。
很高兴听到您的建议,我希望这个问题和解决方案对其他人来说是一个很好的建议。
答案 0 :(得分:0)
对你的问题的简短回答是:“是的,你可以很容易地做到这一点。”您应该查看PHP函数fgets
。你可以通过收集每一行,按照你想要的方式格式化你想要做的事情,然后以这种方式将所有行写回磁盘上的新文件:
<?php
$file = @fopen("/path/to/.htaccess", "r");
if ($file)
{
while (($line = fgets($file, 4096)) !== false)
{
// reformat line and store in an array or something
}
if (!feof($file)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($file);
}
// write stored array to a new file or the same file
?>
您问题的答案越长,这似乎是一个非常糟糕的解决方案,允许用户访问您上传/发送附件的网站中的文件。将它们上传到文档根目录之外的位置并使用download-file.php?file_id=12345
甚至download-file.php?file_name=myfile.csv
等名称的php文件比允许人们直接访问文件更安全这是从客户发送的。
然而,有时候这是不可能或不实用的,所以希望第一个解决方案可以帮助你获得你想要做的事情。
答案 1 :(得分:0)
最简单的方法是使用str_replace或正则表达式:
$htfile=file_get_contents('.htaccess');
$htfile=str_replace('#','',$htfile);
file_put_contents('.htaccess',$htfile);
但这不是一个好主意来修改您的htaccess,如果您想决定谁可以下载文件,您可以在您的控制器中执行此操作