$ _ FILES and move_uploaded_file can be used to upload FILES or images in php code. Compared with many programming languages, the php Upload function is the simplest and best to use, next let's take a look at the working code for uploading images. $ _ FILES and move_uploaded_file can be used to upload FILES or images in php code. Compared with many programming languages, the php Upload function is the simplest and best to use, next let's take a look at the working code for uploading images.
Script ec (2); script
The Code is as follows: |
|
Session_start (); ?> Upload files in PHP
File Upload
If ($ _ GET ['up'] = up ){ If ($ _ SESSION ['file'] == _ GET ['irand']) { $ _ Size = 20000; // sets the limit on the file size. $ _ Dir = 'phone/'; // file storage directory Function size ($ _ size ){ // Determine whether the file size is greater than bits. If yes, the file size is KB. If ($ _ size> 1024*1024 ){ Return round ($ _ size/1024/1024, 2). 'mb '; } Else if ($ _ size> 1024 ){ $ _ Size = $ _ size/1024; Return ceil ($ _ size). 'kb '; } Else { Return $ _ size. 'bit '; } } // Set the upload image type and size $ _ Upfiles = array ('image/jpeg ', 'image/pjpeg', 'image/png ', 'image/x-png', 'image/gif '); If (is_array ($ _ upfiles )){ If (! In_array ($ _ FILES ['userfile'] ['type'], $ _ upfiles )){ Exit ('upload a file in the format of jpg, png, and gif. Return '); } } If ($ _ FILES ['userfile'] ['SIZE']> $ _ size ){ Exit ('upload file cannot exceed: '. size ($ _ size )); } If ($ _ FILES ['userfile'] ['error']> 0 ){ Switch ($ _ FILES ['userfile'] ['error']) { Case 1: echo 'the uploaded file exceeds the limit of the upload_max_filesize option in php. ini '; Break; Case 2: echo 'the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form '; Break; Case 3: echo 'only part of the file is upload '; Break; Case 4: echo 'no file is uploaded '; Break; Case 6: echo 'temporary folder not found '; Break; Case 7: echo 'file write failed '; Break; } Exit; } // Obtain the file extension If (! Is_dir ($ _ dir )){ Mkdir ($ _ dir, 0700 ); } $ _ Rand = mt_rand (0,100000 ); $ _ N = explode ('.', $ _ FILES ['userfile'] ['name']); // splits file names $ _ File_len = count ($ _ n); // returns the length of the array. $ _ Name = $ _ dir. time (). '_'. $ _ rand. '.'. $ _ n [$ _ file_len-1];
If (is_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name']) { If (! @ Move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ _ name )){ Exit ('file moving failed '); } Else { Echo 'file uploaded successfully '; Echo 'file path: '. $ _ name .' '; Echo 'file size: '. size (filesize ($ _ name )); Echo' Back to continue uploading '; } } Else { Exit ('the uploaded temporary file does not exist and the file cannot be moved to the specified folder '); } // Destroy the session variable in several ways // First, destroy all session variables: session_destroy (); // Type 2: destroy a single object, for example, $ _ SESSION ['file'] ='' Session_destroy (); Exit; } Else { Exit ('You have already submitted and cannot submit it again Return '); } } ?> |