来源:未知 时间:2015-08-05 11:26 作者:xxadmin 阅读:次
[导读] 本教程讲解PHP接受文件并获得其后缀名的方法 HTML的form表单 用html的表单模拟一个文件上传的post请求,代码如下: !DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//ENhttp://www.w3.org/TR/html4/loose.dtd...
本教程讲解PHP接受文件并获得其后缀名的方法 HTML的form表单 <!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> <form enctype="multipart/form-data" action="test.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> Send this File:<input name="userfile" type="file"/> <input type="submit" value="Send File" /> </form> </body> </html>
要确保文件上传表单的属性是 enctype="multipart/form-data",否则文件上传不了
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 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta name="keywords" content=" keywords" /> <meta name="description" content="description" /> </head> <body> <form method="post" action="" enctype="multipart/form-data"> <input type="file" name="upfile" size="20" /> <input type="submit" name="submit" value="submit" /> </form> </body> </html>
<?PHP if(isset($_POST['submit'])) { $string = strrev($_FILES['upfile']['name']); $array = explode('.',$string); echo $array[0]; } ?> 结果示例: |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com