File upload function-PHP source code

Source: Internet
Author: User
File upload function code

/*** Upload ** @ allowedTypes array allows you to upload an array of file types (suffix) */function upload ($ inputName, $ allowedSize, $ allowedTypes, $ saveDir) {if (empty ($ _ FILES) {msg ("The size of the uploaded file exceeds the size specified in the form ");} if (empty ($ _ FILES [$ inputName] ['tmp _ name']) {msg ("The size of the uploaded file exceeds the control limit ");} // check whether the upload is legal if (! Is_uploaded_file ($ _ FILES [$ inputName] ['tmp _ name']) {msg ("the uploaded file is invalid ");} // Check the size if ($ _ FILES [$ inputName] ['size']> $ allowedSize) {msg ("The size of the uploaded file exceeds the specified size ");} // Check type $ extension = array_pop (explode (". ", $ _ FILES [$ inputName] ['name']); // suffix if (! In_array ($ extension, $ allowedTypes) {msg ("Upload file type ({$ extension}) is not allowed, only ". implode (",", $ allowedTypes);} // rename $ sp = DIRECTORY_SEPARATOR; if (! File_exists ($ saveDir) {mkdir ($ saveDir, 0777, true);} $ new_filename = $ saveDir. $ sp. date ("YmdHis "). rand (10000,999 99 ). ". ". $ extension; // move $ res = move_uploaded_file ($ _ FILES [$ inputName] ['tmp _ name'], $ new_filename); if ($ res) {return $ new_filename;} else {msg ("file Upload failed ");}}

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.