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 ");}}