2013-06-30 52 views
-2

好了,所以這是我上傳的文件如何允許在PHP腳本上傳僅編程語言擴展

$target = "cloud/"; 
$target = $target . basename($_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename($_FILES['uploaded']['name']). " has been uploaded"; 
mysql_query("INSERT INTO data (`id`,`title`,`url`,`display`) VALUES ('NULL','". basename($_FILES['uploaded']['name']). "','http://test.info/". basename($_FILES['uploaded']['name']). "','1');"); 
} 
else { 
echo "error"; 
} 

我應該添加什麼允許像html的.PHP的.cpp,因此只有擴展腳本?

+0

@jprofitt:我不會依賴$ _FILE ['image'] ['type'],因爲這個值是由瀏覽器發送的,攻擊者可以僞造它。 –

+0

@AmalMurali以下是[幾個](http://stackoverflow.com/questions/12864171/only-allow-jpg-and-png-files-only?lq=1)[other](http://stackoverflow.com/questions/10070465/php-allowable-uploaded-file-extension?lq = 1)[questions](http://stackoverflow.com/questions/10456113/php-check-file-extension-in-upload-form?rq = 1),在這裏可以更好地適應這個問題。 – jprofitt

+0

@jprofitt:他們使用exif_imagetype(僅用於圖像),OP希望檢查擴展名,如html,php等。 –

回答

1

你可以做這樣的事情:

$file_ext = strtolower(pathinfo($_FILES['uploaded']['name'],PATHINFO_EXTENSION)); 
$extensions = ['html', 'php', 'cpp', ...]; 
if ((in_array($file_ext, $extensions)) 
{ 
//do whatever 
} 

我希望這有助於。

相關問題