php上传文件扩展
$f_type=strtolower("swf,rar,jpg,zip,7z,iso,gif,exe,rmvb");//设置可上传的文件类型
if (!is_uploaded_file($file) || ($_FILES['photoupload']['size'] > 20000 * 1024 * 1024) )
{
$error = 'Please upload only files smaller than 2Mb!';
}
$f_name=$_FILES['photoupload']['name'];
$tmp_type=substr(strrchr($f_name,"."),1);//获取文件扩展名
$tmp_type=strtolower($tmp_type);
if(!stristr($f_type,$tmp_type)){
$error="对不起,不能上传格式为:".$tmp_type."的文件:".$f_name;
}
if(move_uploaded_file($_FILES['photoupload']['tmp_name'], $uploadfile)){
$result['result'] = 'success';
$result['size'] =$f_name."文件上传成功";
}
else{
$result['result'] = 'failed';
$result['error'] =$f_name."上传失败";
}
⑵ 关于php实现文件上传
php的文件上传机制是把用户上传的文件保存在php.ini的upload_tmp_dir定义的临时目录(默认是系统的临时目录,如:/tmp)里的一个类似phpxXuoXG的随机临时文件,程序执行结束,该临时文件也被删除。PHP给上传的文件定义了四个变量:(如form变量名是file,而且register_globals打开)
$file #就是保存到服务器端的临时文件(如/tmp/phpxXuoXG )
$file_size #上传文件的大小
$file_name #上传文件的原始名称
$file_type #上传文件的类型
推荐使用:
$_FILES['file']['tmp_name']
$_FILES['file']['size']
$_FILES['file']['name']
$_FILES['file']['type']
⑶ php获取上传文件扩展名$filename= md5(time().rand(0,9).$_FILES["filedata"]["name"]).".bmp";
$filename=md5(time().rand(0,9).$_FILES["filedata"]["name"]).'.'.pathinfo($_FILES["filedata"]["name"],PATHINFO_EXTENSION);
⑷ php 上传文件
刚学php时写的一个类,可以给你参考下,你所说的功能基本上也都有。
这个用作学习还是不错的。
<?php
classfileup{
private$savefilepath;//保存路径
private$filetype=array('gif','jpg','jpeg','png');//文件类型
private$maxsize=1000000;//上传最大的尺寸默认值设置为1M
private$savename=true;//是否默认随机名称
private$upfileform;//上传文件表单的name值
//以下是不可以修改的成员属性
private$tmpname;//上传的临时文件名
private$upfilename;//上传文件的名称
private$uperror;
private$newname;//新的文件名
//private$upfiletype;//上传文件的类型
private$upfilesize;//上传文件的大小。
private$filehz;//文件名的扩展名。
//构造方法
function__construct($upfileform,$savefilepath='./upload/'){
$this->upfileform=$upfileform;
$this->savefilepath=rtrim($savefilepath,'/');
$this->tmpname=$_FILES[$upfileform]['tmp_name'];
$this->upfilename=$_FILES[$upfileform]['name'];
$this->upfilesize=$_FILES[$upfileform]['size'];
$this->uperror=$_FILES[$upfileform]['error'];
$this->getnewname();
}
//设置文件上传的参数,不设置为默认值。
functionsetfilepar($par){
$pars=array('filetype','maxsize','savename');
foreach($paras$key=>$value){
if(in_array($key,$pars)){
$this->$key=$value;
}else{
continue;
}
}
}
//检查上传
privatefunctioncheckfileup(){
//判断文件夹是否正确或文件夹是否有可写入的权限。
if(!is_dir($this->savefilepath)||!is_writable($this->savefilepath)){
$this->uperror=8;
returnfalse;
}
//判断文件名是否存在
if(is_file($this->newname)){
$this->uperror=9;
returnfalse;
}
//判断上传文件的类型是否正确。
if(!in_array(strtolower($this->filehz),$this->filetype)){
$this->uperror=-1;
returnfalse;
}
returntrue;
}
//获取新的文件名字
privatefunctiongetnewname(){
$tmp=explode('.',$this->upfilename);
$this->filehz=$tmp[count($tmp)-1];
if(is_bool($this->savename)){
if($this->savename){
$this->newname=$this->savefilepath.'/'.date('YmdHis').rand(10000,99999).'.'.$this->filehz;
}else{
$this->newname=$this->savefilepath.'/'.$this->upfilename;
}
}else{
$this->newname=$this->savefilepath.'/'.$this->savename.'.'.$this->filehz;
}
}
//获取错误信息
privatefunctiongetuperror(){
switch($this->uperror){
case1:echo'上传文件超过了系统指定的大小';break;
case2:echo'上传文件超过了表单中指定的大小';break;
case3:echo'文件只有部分上传';break;
case4:echo'没有文件上传';break;
case6:echo'找不到上传的文件,系统错误';break;
case7:echo'文件写入失败';break;
case8:echo'文件路径不存在,或不可写';break;
case9:echo'文件名已经存在,请不要重复上传';break;
case-1:echo'不是指定上传的文件';break;
case-2:echo'请勿使用非法途径上传';break;
case-3:echo'文件上传失败';break;
default:'未知错误';break;
}
}
functionfileupload(){
if(!$this->checkfileup()||$this->uperror!=0){
$this->getuperror();
returnfalse;
}else{
if(!is_uploaded_file($_FILES[$this->upfileform]['tmp_name'])){
$this->uperror=-2;
$this->getuperror();
returnfalse;
}else{
if(move_uploaded_file($_FILES[$this->upfileform]['tmp_name'],$this->newname)){
returntrue;
}else{
$this->uperror=-3;
returnfalse;
}
}
}
}
//获取文件名
functiongetname(){
return$this->newname;
}
}
⑸ 在网站设置上上传照片时显示检测到环境未开启php_fileinfo拓展,如何解决
找到配置文件,把这个扩展前面的分号去掉就可以了,前提是已经安装了这个扩展
⑹ 请问在写出php上传组件时该如何限制文件的扩展名
function uploadFile(fileName)
{
var i=fileName.length;
var j=i-4;
var substr=fileName.substring(j,i);
if(substr==".doc"||substr==".xls"||substr==".jpg"){
document.forms[0].action.value="confirm";
document.forms[0].submit();
}
else{
alert("你上传了错误的文件类型,只能上传jpg、excel和word类型的文件");
return false;
}
}
//脚本,但是不安全,最好是交给程序内去判断
1.查找一般的php上传类都可以上传 zip 文件的。 (记得设置好上传文件格式就好)
2.确认你的php扩展中 包含有 php_zip 这个扩展。
然后找 zip 的相关函数方法吧。 php手册中去看。
⑻ php 文件上传 需要开什么扩展
应该不需要,刚刚后盾人讲解过,楼主现在去后盾人学习一下,希望可以帮助你.