PrefaceThis Monday straight again to engage in PHP, involving file upload part of some forgotten, here record
Form form for HTMLUsing HTML forms to simulate a POST request for a file upload, the code is as follows:
File Upload
Send this File:
Attention:
To ensure that the properties of the file upload form are
enctype= "Multipart/form-data", otherwise the file will not upload
PHPFirst, you need to explain the PHP global variable $_files, which contains all the uploaded file information
$_file[' UserFile ' [' Name ']: The original name of the client machine file
$_file[' userfile ' [' type ']: MIME type of File
$_file[' userfile ' [' size ']: Uploaded file size
$_file[' UserFile ' [' Tmpname ']: temporary filename stored on server after file is uploaded
$_file[' userfile ' [' Error ']: and the error code uploaded by the file
Ideas
1, generate 40-bit random string as file name 2, according to the file is picture or voice to the different file location 3, temporarily do not do file size and file type of checksum
function Processfile ($files, $type) {
$uploadName = null;
foreach ($files as $name => $value) {
$originalName = $value [' name '];
$arr = Explode (".", $originalName);
$postfix = $arr [Count ($arr)-1];
$tmpPath = $value [' Tmp_name '];
$tmpType = $value [' type '];
$tmpSize = $value [' size '];
}
$newname = Ehlstaticfunction::generaterandomstr (40). ".". $postfix;
Switch ($type) {case
1:
//process sound file
$destination = Videouploaddir. $newname;
break;
Case 2:
//Processing image file
$destination = Imageuploaddir. $newname;
break;
Move_uploaded_file ($tmpPath, $destination);
}