PHP: How to sanitize uploaded filenames?

后端 未结 6 1935
生来不讨喜
生来不讨喜 2021-01-02 11:32

I have a PHP application.

I allow users to upload files to my web application.

Question: What\'s the best way for me to sanitize the file n

6条回答
  •  甜味超标
    2021-01-02 12:28

    Ciao, this function also removes all the points and then I create the clean string with the extension.

    function sanitaze_upload_file($data)
    {
        $imgName   = $data;
        $indexOFF  = strrpos($imgName, '.');
        $nameFile  = substr($imgName, 0,$indexOFF);
        $extension = substr($imgName, $indexOFF);
        $clean     = preg_replace("([^\w\s\d\-_~,;\[\]\(\)])", "", 
        $nameFile);
        $NAMEFILE  = str_replace(' ', '', $clean).$extension;
        return $NAMEFILE;
    }
    

提交回复
热议问题