php对文件扩展区分大小写

时间:2015-12-29 18:03:33

标签: php file case

现在我有一个成功获取用户想要上传的文件并将其存储在数据库中的php。这些文件必须是(wav,mp3,aiff)但是当它们上传时显然Mp3,AIFF,Wav等不可接受...我如何关闭这种区分大小写?我发布了我的代码以防它可能有所帮助,我不是故意吓唬读者。

<?

// Max size PER file in KB
$max_file_size="5000";

// Max size for all files COMBINED in KB
$max_combined_size="10000";

//Maximum file uploades at one time
$file_uploads="10";

//The name of your website
$websitename="Samplepackgenerator";

// Full browser accessable URL to where files are accessed. With trailing slash.
$full_url="http://YOUR_SITE/uploads/";

// Path to store files on your server If this fails use $fullpath below. With trailing slash.
$folder="./uploads/";

// Use random file names? true=yes (recommended), false=use original file name.
// Random names will help prevent files being denied because a file with that name already exists.
$unique_name=true;

// Types of files that are acceptiable for uploading. Keep the array structure.
$allow_types=array("aiff","mp3","wav");


// Only use this variable if you wish to use full server paths. Otherwise leave this empty. With trailing slash.
$fullpath="";

//Use this only if you want to password protect your upload form.
$password=""; 

// Initialize variables
$password_hash=md5($password);
$error="";
$success="";
$display_message="";
$file_ext=array();
$password_form="";

// Function to get the extension a file.
// function get_ext($key) { 
//  $key=strtolower(substr(strrchr($key, "."), 1));
//  //$key=str_replace("wav","mp3","aiff",$key);
//  return $key;
//}

// Filename security cleaning. Do not modify.
function cln_file_name($string) {
    $cln_filename_find=array("/\.[^\.]+$/", "/[^\d\w\s-]/", "/\s\s+/", "/[-]+/", "/[_]+/");
    $cln_filename_repl=array("", ""," ", "-", "_");
    $string=preg_replace($cln_filename_find, $cln_filename_repl, $string);
    return trim($string);
}

// If a password is set, they must login to upload files.
If($password) {

    //Verify the credentials.
    If($_POST['verify_password']==true) {
        If(md5($_POST['check_password'])==$password_hash) {
            setcookie("phUploader",$password_hash);
            sleep(1); //seems to help some people.
            header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
            exit;
        }
    }

    //Tally the size of all the files uploaded, check if it's over the ammount. 
    If(array_sum($_FILES['file']['size']) > $max_combined_size*1024) {

        $error.="<b>FAILED:</b> All Files <b>REASON:</b> Combined file size is to large.<br />";

    // Loop though, verify and upload files.
    } Else {
        // Loop through all the files.
        For($i=0; $i <= $file_uploads-1; $i++) {
            //Get the file extension
            $file_ext[$i] = pathinfo($_FILES['file']['name'][$i], PATHINFO_EXTENSION);

            // If the file is a file
            If($_FILES['file']['name'][$i]) {
                // Randomize file names

                $file_name[$i]=1;
                while(1){
                $file_name[$i] =$file_name[$i]+1;
                if (!file_exists("uploads/$file_name[$i].$file_ext[$i]")){ break;}
                }

                // Check for blank file name
                If(str_replace(" ", "", $file_name[$i])=="") {

                    $error.= "<b>FAILED:</b> ".$_FILES['file']['name'][$i]." <b>REASON:</b> Blank file name detected.<br />";

                //Check if the file type uploaded is a valid file type. 
                }   
                ElseIf(!in_array($file_ext[$i], $allow_types)) {
                $error.= "<b>FAILED:</b> ".$_FILES['file']['name'][$i]." <b>REASON:</b> Invalide file type.<br />";
                }               
                Elseif($_FILES['file']['size'][$i] > ($max_file_size*1024)) {

                    $error.= "<b>FAILED:</b> ".$_FILES['file']['name'][$i]." <b>REASON:</b> File to large.<br />";

                // Check if the file already exists on the server..
                } 
                Elseif(file_exists($folder.$file_name[$i].".".$file_ext[$i])) {

                    $error.= "<b>FAILED:</b> ".$_FILES['file']['name'][$i]." <b>REASON:</b> File already exists.<br />";

                } 
                Else {

                    If(move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i].".".$file_ext[$i])) {

                        $success.="<b>SUCCESS:</b> ".$_FILES['file']['name'][$i]."<br />";


                    } Else {
                        $error.="<b>FAILED:</b> ".$_FILES['file']['name'][$i]." <b>REASON:</b> General upload failure.<br />";
                    }

                }
                } // If Files

        } // For

    } // Else Total Size

    If(($error=="") AND ($success=="")) {
        $error.="<b>FAILED:</b> No files selected<br />";
    }

    $display_message=$success.$error;

} // $_POST AND !$password_form
?>

4 个答案:

答案 0 :(得分:2)

变化:

ElseIf(!in_array($file_ext[$i], $allow_types)) {

为:

ElseIf(!in_array(strtolower($file_ext[$i]), $allow_types)) {

这会将文件扩展名中的所有字母转换为小写字母,然后再根据$allow_types中的列表进行检查。

答案 1 :(得分:2)

您可以使用函数strtolower()将名称转换为小写,而不是关闭区分大小写。有关详细信息,请阅读manual

答案 2 :(得分:1)

我相信您只想在$file_ext函数调用中将您的作业包装到strtolower数组中。

像这样...

$file_ext[$i] = strtolower(
  pathinfo($_FILES['file']['name'][$i], PATHINFO_EXTENSION)
);

答案 3 :(得分:1)

哇!哇!简单!

文件扩展名不具有文件类型的可信信息。您必须使用http://php.net/manual/en/function.finfo-file.php(FILEINFO_MIME_TYPE)检查文件的mimetype,因为许多文件可以没有扩展名。

之后你可以从mimetype获得扩展名:Convert MIME type to file Extension PHP