前言:
而今你们对“php上传临时文件”都比较注重,兄弟们都想要知道一些“php上传临时文件”的相关文章。那么小编在网摘上汇集了一些有关“php上传临时文件””的相关资讯,希望我们能喜欢,大家快快来了解一下吧!php文件批量上传。index.html<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <form action="upload.php" method="post" enctype="multipart/form-data"> <label for="file">选择文件:</label> <input type="file" name="file[]" multiple="multiple"> <input type="submit" value="上传"> </form></body></html>upload.php<?phpclass Upload{ private $path = './upload'; //上传路径 private $folder; //子文件夹 private $allowType = array('docx', 'jpg', 'zip', 'rar', 'pdf', 'xlsx','php'); //上传路径 private $maxSize = 1024 * 1024 * 10; //限制文件大小 50MB private $randName = false; //文件名是否随机 private $fileOriName; //源文件名 private $fileNewName; //新文件名 private $tmpFileName; //临时文件名 private $fileSize; //文件大小 private $fileHash; //文件哈希值 private $fileExt; //文件扩展名 private $errorNum; //错误号 private $errorMsg; //错误信息 private $returnInfo; //返回的信息 function upfile() //多文件模式 { if ($_FILES['file']['name'][0] == '') { die('参数错误!'); } if ($_FILES['file']['name']) { for ($i = 0; $i < count($_FILES['file']['name']); $i++) { $this->fileOriName = $_FILES['file']['name'][$i]; //源文件名 $this->tmpFileName = $_FILES['file']['tmp_name'][$i]; //临时文件 $this->fileExt = explode('.', $_FILES['file']['name'][$i]); $this->fileExt = end($this->fileExt); //返回文件扩展名 $this->fileHash = hash_file('md5', $_FILES['file']['tmp_name'][$i]); //获取哈希值 $this->errorNum = $_FILES['file']['error'][$i]; //错误代码 $this->errorMsg = $this->Errmsg($this->errorNum); //错误说明 $this->fileSize = $_FILES['file']['size'][$i]; //文件大小 if ($this->fileSize >= $this->maxSize) { $this->errorNum = -1; $this->errorMsg = $this->Errmsg($this->errorNum); $this->info(); //返回上传信息 } elseif (!in_array($this->fileExt, $this->allowType)) { $this->errorNum = -2; $this->errorMsg = $this->Errmsg($this->errorNum); $this->info(); //返回上传信息 } else { $this->creatfolder($this->path); //创建文件夹 $this->folder = $this->path . '/' . date('Ymd'); //子文件夹名称 $this->fileNewName = $this->Rename($this->randName); //生成随机文件名 if (is_file($this->folder . '/' . $this->fileNewName)) { $this->errorNum = -3; $this->errorMsg = $this->Errmsg($this->errorNum); $this->info(); //返回上传信息 } else { $this->creatfolder($this->folder); //创建子文件夹 $this->movefile($this->tmpFileName, $this->folder . '/' . $this->fileNewName); //移动文件 $this->info(); //返回上传信息 } } } } else { echo '系统错误!'; } return $this->returnInfo; } // 返回上传信息 function info() { if ($this->errorNum == 0) { //文件上传成功 $this->returnInfo[] = array( 'fileoriname' => $this->fileOriName, 'fileext' => $this->fileExt, 'tmpfilename' => $this->tmpFileName, 'filenewname' => $this->fileNewName, 'filesize' => $this->fileSize, 'filehash' => $this->fileHash, 'errornum' => $this->errorNum, 'errormsg' => $this->errorMsg ); } else { //文件上传失败 $this->returnInfo[] = array( // 'fileoriname' => $this->fileOriName, // 'fileext' => $this->fileExt, // 'tmpfilename' => $this->tmpFileName, // 'filenewname' => '', // 'filesize' => $this->fileSize, // 'filehash' => $this->fileHash, 'errornum' => $this->errorNum, 'errormsg' => $this->errorMsg ); } } //获取文件哈希值 function tohash($file) { if (is_file($file)) { return hash_file('md5', $file); } else { return true; } } //移动文件 function movefile($tempfile, $desfile) { if (is_file($tempfile) || !is_file($desfile)) { move_uploaded_file($tempfile, $desfile); } else { $this->errorNum = -3; } } //转换文件大小 function tosize($file_size) { if ($file_size >= pow(1024, 4)) { $show_filesize = number_format(($file_size / pow(1024, 4)), 2) . " TB"; } elseif ($file_size >= pow(1024, 3)) { $show_filesize = number_format(($file_size / pow(1024, 3)), 2) . " GB"; } elseif ($file_size >= pow(1024, 2)) { $show_filesize = number_format(($file_size / pow(1024, 2)), 2) . " MB"; } elseif ($file_size >= pow(1024, 1)) { $show_filesize = number_format(($file_size / pow(1024, 1)), 2) . " KB"; } elseif ($file_size >= 0) { $show_filesize = $file_size . " b"; } return $show_filesize; } //错误说明 function Errmsg($error_num) { switch ($error_num) { case 0: $err_msg = '没有错误发生,文件上传成功!'; break; case 1: $err_msg = '上传的文件超过了php.ini 中 upload_max_filesize 选项限制的值!'; break; case 2: $err_msg = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!'; break; case 3: $err_msg = '文件只有部分被上传!'; break; case 4: $err_msg = '没有文件被上传!'; break; case 5: $err_msg = '官方未给出错误说明!'; break; case 6: $err_msg = '找不到临时文件夹!'; break; case 7: $err_msg = '文件写入失败!'; break; case -1: $err_msg = '文件超过允许大小!'; break; case -2: $err_msg = '不是允许的文件类型!'; break; case -3: $err_msg = '目标文件已存在!'; break; default: break; } return $err_msg; } //创建文件夹 function CreatFolder($f) { if (!is_dir($f)) { mkdir($f, 0777); } else { return true; } } //随机文件名 function Rename($rand) { if ($rand) { //true $arr = array_merge(range('A', 'Z'), range('1', '9')); $randstr = ''; for ($i = 0; $i <= 8; $i++) { $a = rand(0, count($arr) - 1); $randstr .= $arr[$a]; } $rename = date('YmdHis') . '_' . $randstr . '.' . $this->fileExt; } else { $rename = $this->fileOriName; } return $rename; }}//类的调用$upload = new Upload();echo '<pre>';var_dump($upload->upfile());
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #php上传临时文件