今天改进了下旗下几个网站的文件上传系统,顺便发点东西,全php代码,无js,文件类型根据后缀名判断,非mime判断,新建个up.php,代码如下:
- <?php
- $uptype=array("jar","zip");
-
- $max_file_size=20480000;
- $path_parts=pathinfo($_SERVER['PHP_SELF']);
- $destination_folder="files/";
-
- $name="MuXi_".date("Y-m-d_H-i-s");
-
- if($_SERVER['REQUEST_METHOD'] == 'POST')
- {
- $file = $_FILES["upload_file"];
- if(!is_uploaded_file($file["tmp_name"]))
-
- {
- echo "文件不存在!";
- exit;
- }
- $torrent = explode(".", $file["name"]);
- $fileend = end($torrent);
- $fileend = strtolower($fileend);
- if(!in_array($fileend, $uptype))
-
- {
- echo"不允许上传此类型文件!";
- exit;
- }
- if($max_file_size < $file["size"])
-
- {
- echo "文件太大,超过上传限制!";
- exit;
- }
- if(!file_exists($destination_folder))
- mkdir($destination_folder);
- $filename=$file["tmp_name"];
- $image_size = getimagesize($filename);
- $pinfo=pathinfo($file["name"]);
- $ftype=$pinfo[extension];
- $destination = $destination_folder.$name.".".$ftype;
- if(file_exists($destination) && $overwrite != true)
- {
- echo "同名文件已经存在了!";
- exit;
- }
- if(!move_uploaded_file ($filename, $destination))
- {
- echo "移动文件出错!";
- exit;
- }
- $pinfo=pathinfo($destination);
- $fname=$pinfo[basename];
- echo "上传成功!";
- }
- ?>
调用代码如下:
- <form action="up.php" method="post" enctype="multipart/form-data">
- <input type="file" name="upload_file" />
- <input type="submit" value="上传" />
用mime类型限制有局限性,有些文件在上传是不是正常本身的mime,导致上传不成功,而用后缀名限制可以很好的解决这个问题. |