2014-01-05 167 views
1

我正在使用此脚本从我的服务器中删除图片。但同时我想保护我的服务器中的文件。不小心删除,但我注意到,如果我输入文件index.pHpindex.Php已从我的服务器中删除。虽然设置它不会删除为什么PHP或此方法不知道小写和大写之间。检测小写字母和大写字母php

什么是不正确的?

<?php 
error_reporting (0); 
$thefile = $_GET ['filetodel']; 
$filename = "$thefile"; 
//$filename = "picture1.jpg"; 

/*protect some files*/ 
if ($thefile=='index.php' or $thefile=='INDEX.PHP' or $thefile=='UPLOADS.ZIP' or $thefile=='uploads.zip' or $thefile=='del.php'or $thefile=='DEL.PHP' or $thefile==NULL or $thefile=='.htaccess' or $thefile=='.HTACCESS') 
{ 
exit("<h2>cannot delete $thefile</h2>"); 
} 
if ($thefile=="$thefile") 
{ 
if (file_exists($filename)) 
{ 
unlink ("$thefile"); 
echo "<h2> file $thefile is delete</h2>"; 
} 
else 
{ 
echo "<h2>The<br>"; 
echo "$filename<br>"; 
echo "Does not exist</h2>"; 
} 
} 
?> 
+1

所以你知道在未来,你的代码必须在第三方服务的问题上进行。 – meagar

回答

4

只是将输入小写和一次测试,而不用担心案件的每一个可能的组合:

if (strtolower($thefile) == 'index.php') { 
    // ... 
} 

对于下一个迭代,你可以在你的受保护的文件存储在一个阵列:

$protected_files = array('index.php', 'uploads.zip', 'del.php', '.htaccess'); 

if (in_array(strtolower($thefile), $protected_files) || $thefile==NULL) { 
    // ... 
} 
+1

对于63.6k的用户,HTML不是一个很好的退出字符串参数。 –

+0

错误,这是他的代码复制粘贴。 “退出”电话与被问到的问题并不相关。 – meagar

+0

谢谢你的工作 – manny

1

的问题是在这里:

if ($thefile=="$thefile") 

,就好像你的文件检查第一个条件比第二条件的错误是

if ($thefile=="$thefile") 

这始终是true,因此它会断开链接的文件 也只需1条件之前添加一行如下

$thefile = strtolower($thefile); 
+0

感谢您的评论,我会修复它 – manny

+0

只是修改了答案以满足您的第一个条件 – dev