2013-03-13 56 views
0

我編寫了一個文件上傳腳本,我想檢查上傳的文件是否爲PHP。如何檢查上傳的文件是否是php?

我想保護我的腳本,以便允許上傳的唯一文件是CSS和XML。

這是我的代碼:

<form action="cp_home.php?mode=up_styles&type=uploading" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
    <input class="fixed" name="ufile" type="file" id="ufile" size="35" /> 
    <input type="button" name="Submit" onclick="upload()" value="up style" /> 
</form> 

回答

0

你可以檢查擴展名,因爲只有PHP擴展文件將由PHP執行。

$info = pathinfo(basename($_FILES['image']['name'])); 
$ext = strtolower($info['extension']); 

if ($ext != 'php') { 
    // process file upload 
} 

,或者如果你PHP3或PHP5文件擴展名的服務器支持,你可能需要使用

if (!preg_match('/^php/', $ext)) { 
    // process file upload 
} 
+0

請注意,如果啓用了Apache的MultiViews功能,它將查看所​​有擴展名,並將執行(例如)'foo.php.en'作爲PHP。 – Boann 2013-03-13 13:23:01

0

在PHP代碼檢查延期:

$allowedExts = array("css", "xml"); // css and xml are allowed here, if you want more add it here. 
$extension = end(explode(".", $_FILES["file"]["name"])); 

if(in_array($extension, $allowedExts)){ 

    // your code here 

} 
+0

嚴格的標準:只有變量應參考 – Touki 2013-03-13 12:58:16

+0

傳遞任何人都可以改變外殼的名字'shell.php。 css'並上傳它的shell將工作 – user2036850 2013-03-13 12:59:04

+0

@ user2036850不僅PHP文件將被php執行。 – jcubic 2013-03-13 12:59:53

-1
var x = "1.txt"; 
alert (x.substring(x.lastindexOf(".")+1)); 

使用此輸入表單文件的文本。

+0

請注意,用戶可以禁用javacript – jcubic 2013-03-13 13:02:15

0
$allowedExts = array("css", "xml"); 
     $extension = end(explode(".", $_FILES["file"]["name"])); 

     if (in_array($extension, $allowedExts)) { 

      if (move_uploaded_file($_FILES['file']["tmp_name"], PATH.$name)) { 

       if (file_exists($name . $extension)) { 
        echo "uploaded"; 
       } 
       else{ 
        echo "not"; 
       } 
      } 
     } 
0

兩種方法來檢查它

1.You可以檢查它的JavaScript本身

function is_php(filename){ 
return filename.split('.').pop()=="php"; 
} 

2.Or el本身你可以簡單地檢查了後臺。例如如果您使用PHP本身,然後使用下面的代碼

$path_parts = pathinfo($filename); 
if($path_parts['extension']!="php") 
return false; 
else 
return true; 
相關問題