Home > php教程 > php手册 > body text

使用PHP接受文件并获得其后缀名的方法

WBOY
Release: 2016-06-06 19:48:44
Original
1180 people have browsed it

这篇文章主要介绍了使用PHP接受文件并获得其后缀名的方法,作者着重提到了其中$_FILES全局变量的使用,需要的朋友可以参考下

HTML的form表单
用html的表单模拟一个文件上传的post请求,代码如下:

File Upload
Send this File:


注意:

要确保文件上传表单的属性是 enctype="multipart/form-data",否则文件上传不了


PHP
首先,需要解释一下PHP的全局变量$_FILES,此数组包含了所有上传的文件信息


思路
1、生成40位的随机字符串作为文件名
2、根据文件是图片还是语音转存到不同的文件位置
3、暂时不做文件大小和文件类型的校验

 

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); }

而获取所上传文件的后缀名则可以使用一下代码:

HTML


PHP

结果示例:

20158591330708.jpg (423×167)

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!