One PHP written by yourself

xiaoxiao2021-03-06  41

Main features: File upload, get file name, get file size, randomly generate new file names, get file type, image generate thumbnail, return to the thumbnail file name, return the file name of the file generated after the upload, return the file back path

/ / -------------------------------------------------------------------------------------------- ---------------------- // Please keep this statement information, this declaration will not affect your speed! // ***** ************** IEB upload class V1.1 *************************************** *** // author: Lu Weihua // Web site: http: //www.iebsoft.cn// E-mail: yagas60@21cn.com// copyright notice: Copyright, open source code, available for free use for various purposes , But you must send a modified file // to the author after modification. //*************************************** *************************************** //

Class IEB_UPLOAD {var $ FORMNAME; // File Domain Name VAR $ Directroy; // Upload to Directory VAR $ MAXSIZE; / / Maximum Upload Size Var $ CANUPLOAD; / / Can I upload Var $ DOUPFILE; // Uploaded file name var $ SM_FILE; // Thumbnail Name Var $ Error; // Error Parameter Function IEB_UPLOAD ($ FORMNAME = ', $ DIRPATH =', $ MAXSIZE = 2097152) / / (1024 * 2) * 1024 = 2097152 is 2M { global $ FormName, $ Directroy, $ MaxSize, $ CanUpload, $ Error, $ doUpFile, $ sm_File; // initialize various parameters $ FormName = $ formName; $ MaxSize = $ maxSize; $ CanUpload = true; $ doUpFile = '' ; $ = = 0; if ($ formname == ') {$ canupload = false; $ error = 1; Break;} if ($ DIRPATH ==') {$ DirecTroy = $ DIRPATH } Else {$ directroy = $ dirPath. '/';}} // Check the file is Function ScanFile () {Global $ FORMNAME, $ ERROR, $ CANUPLOAD; if ($ canupload) {$ SCAN = IS_READABLE ($ _ files [$ FORMNAME] ['Name']); if ($ scan) {$ error = 2;} Return $ scan;}} // Get file size Function GetSize ($ format = 'b') {Global $ FORMNAME, $ Error, $ CANUPLOAD; IF ($ CANUPLOAD) {IF ($ _files [$ FORMNAME] ['size'] == 0) {$ Error = 3; $ canupload = false;} switch ($ format) {case 'b': return $ _files [$ formname] ['size']; break; case 'm': return ($ _files [$ flmname ] ['size']) / (1024 * 1024);}}} // Get file type function getext () {Global $ FORMNAME, $ ERROR, $ CANUPLOAD; if ($ CANUPLOAD) {$ ext = $ _ files [$ Formname] ['Name']; $ extstr = expede ('.', $ Ext); $ count = count ($ extstr) -1;} Return $ extstr [$ count];} // Get file name Function GetName ) {Global $ FORMNAME, $ CANUPLOAD;

IF ($ CANUPLOAD) {RETURN $ _FILES [$ FORMNAME] ['name'];}} // New File name Function newname () {Global $ CANUPLOAD, $ FORMNAME; if ($ canupload) {$ 50 {$ 50 = $ _ files [ $ FORMNAME] ['Name']; $ extstr = expedition (', $ fullname); $ count = count ($ extstr) -1; $ extstr [$ count]; returnate (' ymdhis') . '.' $ ext;} // Upload file Function Upload ($ filename = ') {Global $ FORMNAME, $ Directroy, $ CANUPLOAD, $ ERROR, $ DOUPFILE; if ($ Canupload {if ($ _files [$ formname] ['size'] == 0) {$ error = 3; $ canupload = false; returnPload = false; return;}} f ($ canupload) {if ($ filename = = ') {$ Filename = $ _files [$ formname] [' name ']; $ doupload = @ copy ($ _ files [$ formname], $ directroy. $ Filename); if ($ DOUPLOAD ) {$ Doupfile = $ filename; chmod ($ DirecTroy. $ Filename, 0777);} else}}}}}}}} // Create a picture thumbnail Function thumb ($ dscchar = ' ', $ width = 150, $ height = 113) {Global $ CANUPLOAD, $ ERROR, $ DIRECTROY, $ DOUPFILE, $ SM _File; if ($ CANUPLOAD && $ DOUPFILE! = ') {$ Srcfile = $ doupFile; if ($ dschar ==') {$ dscchar = 'sm_';} $ dscfile = $ directroy. $ Dschar. $ Srcfile $ Data = GetImagesize ($ INFO); Switch ($ DATA [2]) {Case 1: $ IM = @ImageCreateFromgif ($ DirecTroy. $ Srcfile; Break; Case 2: $ IM = @ImageCreateFromJPEG ($ DirecTroy. $ srcfile; Break; Case 3: $ IM = @ImageCreateFromPng ($ DirecTroy. $ srcfile; Break;} $ srcw = imagesX ($ IM);

$ SRCH = ImageESY ($ IM); $ ni = imagecreatetruecolor ($ Width, $ HEIGHT); ImageCopyResized ($ N, $ IM, 0, 0, 0, 0. Width, $ HEIGHT, $ SRCW, $ SRCH); $ CR = ImageJpeg ($ Ni, $ DSCFILE); Chmod ($ DSCFILE, 0777); if ($ CR) {$ SM_FILE = $ DSCFILE; RETURN TRUE;} else {$ error = 5; return}}}} // Display error parameter function err () {global $ error; returnome;} // uploaded file name Function Upfile () {Global $ DOUPFILE, $ Error; if ($ DOUPFILE! = ') {RETURN $ DoupFile;} else {$ error = 6;}} // Upload files Function FilePath () {Global $ Directroy, $ DOUPFILE, $ Error; IF ($ DOUPFILE! = ') {Return $ Directroy. $ DOUPFILE; } else {$ error = 6;}} // Thumbnail file name function thumbmap () {Global $ sm_file, $ error; if ($ sm_file! = ') {RETURN $ SM_FILE;} else {$ Error = 6; }} // Display version information FUNCTION IEB_VERSION () {RETURN 'IEB_UPLOAD CLASS VER 1.1';}}?> How to use:

getName (). '; // Return file complication name Echo' file type: '. Upfos -> getExt (). '; // Return to file size Echo' file size: '. $ upfos -> getSize ().'; / * getSize (Format) Format: Return to file size Unit value. The default is B. B is byte m for MB: getSize ('b'); * // Randomly generated file name Echo 'random file:'. $ Upfos -> newname (). '; / * Suggestions Randomly generated file names to avoid uploading reputation files. For example: $ upfos -> Upload ($ upfos -> newname ()); * / / Upload file $ upfos -> upload (); / * UPLOAD (filename) FileName: Generate this file name after uploading to the server. The default is the original file name. * / / Generate thumbnails $ upfos -> thumb (); / * thumb ([Key, Width, Height]) Key: Generates a keyword for the thumbnail. The default is "SM_".

转载请注明原文地址:https://www.9cbs.com/read-54509.html

New Post(0)