简体   繁体   English

上传前如何使用php获取安全文件名

[英]how to get secure file name with php before upload

I replace the file name with a random string, a hash (md5/sha1),uniqid() or a timestamp or etc ..... 我将文件名替换为随机字符串,哈希(md5 / sha1),uniqid()或时间戳记等。

Example Using uniqid(): 使用uniqid()的示例:

9d24707b98e4ddfae9e321ef4f502241.jpg 

Example wordpress sanitiza file name function: 示例wordpress sanitiza文件名功能:

function sanitize_file_name( $filename ) {
            $filename_raw = $filename;
            $special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
            /**
             * Filter the list of characters to remove from a filename.
             *
             * @since 2.8.0
             *
             * @param array  $special_chars Characters to remove.
             * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
             */
            $special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
            $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
            $filename = str_replace($special_chars, '', $filename);
            $filename = preg_replace('/[\s-]+/', '-', $filename);
            $filename = trim($filename, '.-_');

            // Split the filename into a base and extension[s]
            $parts = explode('.', $filename);

        // Return if only one extension
            if ( count( $parts ) <= 2 ) {
                    /**
                     * Filter a sanitized filename string.
                     *
                 * @since 2.8.0
                     *
                     * @param string $filename     Sanitized filename.
                * @param string $filename_raw The filename prior to sanitization.
                     */
                    return apply_filters( 'sanitize_file_name', $filename, $filename_raw );
            }

            // Process multiple extensions
            $filename = array_shift($parts);
            $extension = array_pop($parts);
            $mimes = get_allowed_mime_types();

            /*
             * Loop over any intermediate extensions. Postfix them with a trailing underscore
             * if they are a 2 - 5 character long alpha string not in the extension whitelist.
             */
            foreach ( (array) $parts as $part) {
                    $filename .= '.' . $part;

                    if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
                            $allowed = false;
                            foreach ( $mimes as $ext_preg => $mime_match ) {
                                    $ext_preg = '!^(' . $ext_preg . ')$!i';
                                    if ( preg_match( $ext_preg, $part ) ) {
                                            $allowed = true;
                                            break;
                                    }
                            }
                            if ( !$allowed )
                                    $filename .= '_';
                    }
            }
            $filename .= '.' . $extension;
            /** This filter is documented in wp-includes/formatting.php */
            return apply_filters('sanitize_file_name', $filename, $filename_raw);
    }

my way is secure/safe way Or I need to Sanitize file name with any class/function Or Both Way Combination ? 我的方法是安全/安全的方法还是需要使用任何类/函数对文件名进行消毒或双向组合?

The best thing to do is to have an on-disk file name that isn't based off any predictable data by the user. 最好的做法是拥有一个磁盘文件名,该文件名不基于用户的任何可预测数据。 You can simply use the ID number of the asset as its metadata in your database. 您可以简单地使用资产的ID号作为数据库中的元数据。 No file name extension. 没有文件扩展名。 Don't leave it in the web server's document root. 不要将其保留在Web服务器的文档根目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM