The HTML form uses the html form to simulate a post request for file upload. The code is as follows:
File UploadSend this File:
Note:
Make sure the file upload form is The attribute is enctype="multipart/form-data", otherwise the file cannot be uploaded
First of all, you need to explain the PHP global variable $_FILES. This array contains all 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'] : The temporary file name stored on the server after the file is uploaded
$_FILE['userfile']['error'] : Upload with the file Error code
Ideas
1. Generate a 40-digit random string as the file name
2. Transfer the file to different locations depending on whether it is a picture or a voice. File location
3. File size and file type verification will not be performed for the time being
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 : // 处理声音文件 $destination = VIDEOUPLOADDIR.$newname; break; case 2 : // 处理图像文件 $destination = IMAGEUPLOADDIR.$newname; break; } move_uploaded_file($tmpPath, $destination); }
For more related tutorials, please visit PHP programming from entry to master full set of videos Tutorial