How does this function determine the format of the uploaded file?

Source: Internet
Author: User
How does this function determine the format of the uploaded file?
function f_postfix($f_type,$f_upfiles){$is_pass = false;$tmp_upfiles = split("\.",$f_upfiles);$tmp_num = count($tmp_upfiles);for($num = 0; $num < count($f_type);$num++){if(strtolower($tmp_upfiles[$tmp_num - 1]) == $f_type["$num"])$is_pass = $f_type["$num"];}return $is_pass;}


Reply to discussion (solution)

$ F_type what is this? If you do not know what the function parameters mean, you can analyze the function literally.

$ Is_pass = false; // declare traversal
$ Tmp_upfiles = split ("\.", $ f_upfiles); // string cut array
$ Tmp_num = count ($ tmp_upfiles); // Number of arrays of the same level
For ($ num = 0; $ num <count ($ f_type); $ num ++) {// traverses the array
If (strtolower ($ tmp_upfiles [$ tmp_num-1]) = $ f_type ["$ num"]) // numerical comparison
$ Is_pass = $ f_type ["$ num"]; // assign a value again
}
Return $ is_pass; // return result

$ F_type should be the upload type, array, lower case, subscript starts from 0
$ Tmp_upfiles = split ("\.", $ f_upfiles); // splits the input file name (or path) into an array using "."

Here$ Tmp_upfiles [$ tmp_num-1]
I don't understand. how can I understand it logically?

Function f_postfix ($ f_type, $ f_upfiles) {$ is_pass = false; // specify the initial value of the returned value. false indicates that $ tmp_upfiles = split ("\. ", $ f_upfiles); // Press. split the file name into an array // Note: The split function has been abolished since php5.3, and the @ blocking error must be added during use. // We recommend that you change it to explode, $ tmp_upfiles = explodet (". ", $ f_upfiles); $ tmp_num = count ($ tmp_upfiles); // count the number of array elements after splitting ($ num = 0; $ num <count ($ f_type ); $ num ++) {// if (strtolower ($ tmp_upfiles [$ tmp_num-1]) for the input suffix array = $ f_type ["$ num"]) // compare with the extension of the file name $ is_pass = $ f_type ["$ num"]; // Save The Result} return $ is_pass; // return the calculation result}

Consider dividing the string into an array and getting the last element and
Substr ($ f_upfiles, strripos ($ f_upfiles, '.') + 1 );
It is equivalent and does not need to be split multiple times.

The suffix of extracted file names is more
Pathinfo ($ f_upfiles, PATHINFO_EXTENSION)
Simple form
Therefore, the function can be rewritten
function f_postfix($f_type,$f_upfiles) {  $ext = strtolower(pathinfo($f_upfiles, PATHINFO_EXTENSION));  return in_array($ext, $f_type) ? $ext : false;}

$ Tmp_upfiles = array (1.2.3. 4 );
$ Tmp_num = count ($ tmp_upfiles); // 4

$ Tmp_upfiles [$ tmp_num-1] is
$ Tmp_upfiles [4-1] = $ tmp_upfiles [3]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.