Foreword
I've been working on PHP this week, and I've forgotten a bit about file uploading. I'll record it here.
HTML form
Use an HTML form to simulate a post request for file upload. The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Upload</title>
</head>
<body>
</body>
</html>
Notice:
Make sure the attribute of the file upload form is
enctype="multipart/form-data", otherwise the file cannot be uploaded.
PHP
First, we need to explain PHP’s 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 the 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'] : and the error code for uploading the file
Thoughts
1. Generate a 40-digit random string as the file name
2. Transfer the file to different file locations depending on whether it is a picture or a voice.
3. No verification of file size and file type is 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);
}
http://www.bkjia.com/PHPjc/664289.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/664289.htmlTechArticlePreface I have been working on PHP this week, and I have forgotten the part involving file upload. Here is the HTML form. Use an HTML form to simulate a post request for file upload. The code is as follows...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn