簡體   English   中英

如何檢查上傳的文件是否是php?

[英]how to check if uploaded file is 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>

在您的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

}

您可以檢查擴展名,因為僅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
}
 $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";
                }
            }
        }

兩種檢查方法

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

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

2.否則,您可以直接在后端簽入。例如,如果您使用的是php本身,則使用以下代碼

$path_parts = pathinfo($filename);
if($path_parts['extension']!="php")
return false;
else 
return true;
var x = "1.txt";
alert (x.substring(x.lastindexOf(".")+1));

在輸入表單文件文本上使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM