Defining prompt functions Function alert ($msg) { Return '; } Define the allowed file types
$allowType = Array (' Image/jpeg ', ' image/gif ', ' image/jpg '); Define a path, either an absolute path, or a relative path can be
$filePath = './uploadfiledir/'; Receive form information The file value written inside it is the name value in the static page form $file = $_files[' file ']; The first step is to determine if the uploaded file has errors if ($file [' ERROR ']!== 0) { Exit (Alert (' File upload error '); } The second step, determine the file size, here is 102400 bytes, converted to KB is 100KB if ($file [' Size '] > 102400) { Exit (Alert (' File too large '); } Step three, determine the file type if (!in_array (Mime_content_type ($file [' tmp_name ']), $allowType)) { Exit (Alert (' File type error ')); } The fourth step is to determine if the path exists, and if it does not exist, create if (!file_exists ($filePath) &&!mkdir ($filePath, 0777,true)) { Exit (Alert (' Create directory error ')); } Fifth step, define the name and path after uploading $filename = Time (). ' _ '. $file [' name ']; Sixth step, copy the file if (!copy ($file [' Tmp_name '], $filePath. $filename)) { Exit (Alert (' Error uploading file, please retry later ')); } Seventh step, delete temporary files Unlink ($file [' tmp_name ']); Prompt for upload success echo alert (' Congratulations, upload file ['. $filename. '] Success! '); ?> |