2014-01-05 166 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