㈠ python如何获取上传图片后缀名
1. 获取文件后缀名:
复制代码代码如下:
#!/usr/bin/python
import os
dict = {}
for d, fd, fl in os.walk('/home/ahda/Program/'):
for f in fl:
sufix = os.path.splitext(f)[1][1:]
if dict.has_key(sufix):
dict[sufix] += 1
else:
dict[sufix] = 1
for item in dict.items():
print "%s : %s" % item
这里的关键是os.path.splitext()
如abc/ef.g.h ,这里获取到的是h
2. python查找遍历指定文件路径下指定后缀名的文件实例:
复制代码代码如下:
import os
import sys
import os.path
for dirpath, dirnames, filenames in os.walk(startdir):
for filename in filenames:
if os.path.splitext(filename)[1] == '.txt':
filepath = os.path.join(dirpath, filename)
#print("file:" + filepath)
input_file = open(filepath)
text = input_file.read()
input_file.close()
output_file = open( filepath, 'w')
output_file.write(text)
output_file.close()
3. 批量重命名目录中的文件后缀实例:
复制代码代码如下:
import os
def swap_extensions(dir, before, after):
if before[:1] != '.': #如果参数中的后缀名没有'.'则加上
before = '.' + before
thelen = -len(before)
if after[:1] != '.':
after = '.' + after
for path, subdir, files in os.walk(dir):
for oldfile in files:
if oldfile[thelen:] == before:
oldfile = os.path.join(path, oldfile)
newfile = oldfile[:thelen] + after
os.rename(oldfile, newfile)
print oldfile +' changed to ' + newfile
if __name__ == '__main__':
import sys
if len(sys.argv) != 4:
print 'Usage:swap_extension.py rootdir before after'
sys.exit(1)
swap_extensions(sys.argv[1], sys.argv[2], sys.argv[3])
例子:将e:/py/test目录下.php结尾的文件重命名为.py
E:py>python_cook e:/py/test .php .py
e:/py/testtest.php changed to e:/py/testtest.py
e:/py/test1.php changed to e:/py/test1.py
e:/py/test2.php changed to e:/py/test2.py
㈡ php上传图片没有文件名后缀
$filename = basename($_FILES['image']['name']);
$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
echo $file_ext;
请楼主先试试用这几句看是否能获取到后缀名
一般来说,即使系统没有显示后缀名,在php上传中也是可以获取到后缀名的
还有,就是要检查一下,move_uploaded_file函数里,目标地址的图片名称是否拼接了后缀名
㈢ thinkphp5 怎么接受zyupload,上传的图片
引入这个类就可以
<?php
//视图表单
//支持多张图片上传
classupload{
var$dir;//附件存放物理目录
var$time;//自定义文件上传时间
var$allow_types;//允许上传附件类型
var$field;//上传控件名称
var$maxsize;//最大允许文件大小,单位为KB
var$thumb_width;//缩略图宽度
var$thumb_height;//缩略图高度
var$watermark_file;//水印图片地址
var$watermark_pos;//水印位置
var$watermark_trans;//水印透明度
//构造函数
//$types:允许上传的文件类型,$maxsize:允许大小,$field:上传控件名称,$time:自定义上传时间
functionupload($types='jpg|png',$maxsize=1024,$field='attach',$time=''){
$this->allow_types=explode('|',$types);
$this->maxsize=$maxsize*1024;
$this->field=$field;
$this->time=$time?$time:time();
}
//设置并创建文件具体存放的目录
//$basedir:基目录,必须为物理路径
//$filedir:自定义子目录,可用参数{y}、{m}、{d}
functionset_dir($basedir,$filedir=''){
$dir=$basedir;
!is_dir($dir)&&@mkdir($dir,0777);
if(!empty($filedir)){
$filedir=str_replace(array('{y}','{m}','{d}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));//用string_replace把{y}{m}{d}几个标签进行替换
$dirs=explode('/',$filedir);
foreach($dirsas$d){
!empty($d)&&$dir.=$d.'/';
!is_dir($dir)&&@mkdir($dir,0777);
}
}
$this->dir=$dir;
}
//图片缩略图设置,如果不生成缩略图则不用设置
//$width:缩略图宽度,$height:缩略图高度
functionset_thumb($width=0,$height=0){
$this->thumb_width=$width;
$this->thumb_height=$height;
}
//图片水印设置,如果不生成添加水印则不用设置
//$file:水印图片,$pos:水印位置,$trans:水印透明度
functionset_watermark($file,$pos=6,$trans=80){
$this->watermark_file=$file;
$this->watermark_pos=$pos;
$this->watermark_trans=$trans;
}
/*—————————————————————-
执行文件上传,处理完返回一个包含上传成功或失败的文件信息数组,
其中:name为文件名,上传成功时是上传到服务器上的文件名,上传失败则是本地的文件名
dir为服务器上存放该附件的物理路径,上传失败不存在该值
size为附件大小,上传失败不存在该值
flag为状态标识,1表示成功,-1表示文件类型不允许,-2表示文件大小超出
—————————————————————–*/
functionexecute(){
$files=array();//成功上传的文件信息
$field=$this->field;
$keys=array_keys($_FILES[$field]['name']);
foreach($keysas$key){
if(!$_FILES[$field]['name'][$key])continue;
$fileext=$this->fileext($_FILES[$field]['name'][$key]);//获取文件扩展名
$filename=date('Ymdhis',$this->time).mt_rand(10,99).'.'.$fileext;//生成文件名
$filedir=$this->dir;//附件实际存放目录
$filesize=$_FILES[$field]['size'][$key];//文件大小
//文件类型不允许
if(!in_array($fileext,$this->allow_types)){
$files[$key]['name']=$_FILES[$field]['name'][$key];
$files[$key]['flag']=-1;
continue;
}
//文件大小超出
if($filesize>$this->maxsize){
$files[$key]['name']=$_FILES[$field]['name'][$key];
$files[$key]['name']=$filesize;
$files[$key]['flag']=-2;
continue;
}
$files[$key]['name']=$filename;
$files[$key]['dir']=$filedir;
$files[$key]['size']=$filesize;
//保存上传文件并删除临时文件
if(is_uploaded_file($_FILES[$field]['tmp_name'][$key])){
move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);
@unlink($_FILES[$field]['tmp_name'][$key]);
$files[$key]['flag']=1;
//对图片进行加水印和生成缩略图,这里演示只支持jpg和png(gif生成的话会没了帧的)
if(in_array($fileext,array('jpg','png'))){
if($this->thumb_width){
if($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)){
$files[$key]['thumb']='thumb_'.$filename;//缩略图文件名
}
}
$this->create_watermark($filedir.$filename);
}
}
}
return$files;
}
//创建缩略图,以相同的扩展名生成缩略图
//$src_file:来源图像路径,$thumb_file:缩略图路径
functioncreate_thumb($src_file,$thumb_file){
$t_width=$this->thumb_width;
$t_height=$this->thumb_height;
if(!file_exists($src_file))returnfalse;
$src_info=getImageSize($src_file);
//如果来源图像小于或等于缩略图则拷贝源图像作为缩略图,免去操作
if($src_info[0]<=$t_width&&$src_info[1]<=$t_height){
if(!($src_file,$thumb_file)){
returnfalse;
}
returntrue;
}
//按比例计算缩略图大小
if(($src_info[0]-$t_width)>($src_info[1]-$t_height)){
$t_height=($t_width/$src_info[0])*$src_info[1];
}else{
$t_width=($t_height/$src_info[1])*$src_info[0];
}
//取得文件扩展名
$fileext=$this->fileext($src_file);
switch($fileext){
case'jpg':
$src_img=ImageCreateFromJPEG($src_file);break;
case'png':
$src_img=ImageCreateFromPNG($src_file);break;
case'gif':
$src_img=ImageCreateFromGIF($src_file);break;
}
//创建一个真彩色的缩略图像
$thumb_img=@ImageCreateTrueColor($t_width,$t_height);
//ImageCopyResampled函数拷贝的图像平滑度较好,优先考虑
if(function_exists('imageresampled')){
@ImageCopyResampled($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
}else{
@ImageCopyResized($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
}
//生成缩略图
switch($fileext){
case'jpg':
ImageJPEG($thumb_img,$thumb_file);break;
case'gif':
ImageGIF($thumb_img,$thumb_file);break;
case'png':
ImagePNG($thumb_img,$thumb_file);break;
}
//销毁临时图像
@ImageDestroy($src_img);
@ImageDestroy($thumb_img);
returntrue;
}
//为图片添加水印
//$file:要添加水印的文件
functioncreate_watermark($file){
//文件不存在则返回
if(!file_exists($this->watermark_file)||!file_exists($file))return;
if(!function_exists('getImageSize'))return;
//检查GD支持的文件类型
$gd_allow_types=array();
if(function_exists('ImageCreateFromGIF'))$gd_allow_types['image/gif']='ImageCreateFromGIF';
if(function_exists('ImageCreateFromPNG'))$gd_allow_types['image/png']='ImageCreateFromPNG';
if(function_exists('ImageCreateFromJPEG'))$gd_allow_types['image/jpeg']='ImageCreateFromJPEG';
//获取文件信息
$fileinfo=getImageSize($file);
$wminfo=getImageSize($this->watermark_file);
if($fileinfo[0]<$wminfo[0]||$fileinfo[1]<$wminfo[1])return;
if(array_key_exists($fileinfo['mime'],$gd_allow_types)){
if(array_key_exists($wminfo['mime'],$gd_allow_types)){
//从文件创建图像
$temp=$gd_allow_types[$fileinfo['mime']]($file);
$temp_wm=$gd_allow_types[$wminfo['mime']]($this->watermark_file);
//水印位置
switch($this->watermark_pos){
case1://顶部居左
$dst_x=0;$dst_y=0;break;
case2://顶部居中
$dst_x=($fileinfo[0]-$wminfo[0])/2;$dst_y=0;break;
case3://顶部居右
$dst_x=$fileinfo[0];$dst_y=0;break;
case4://底部居左
$dst_x=0;$dst_y=$fileinfo[1];break;
case5://底部居中
$dst_x=($fileinfo[0]-$wminfo[0])/2;$dst_y=$fileinfo[1];break;
case6://底部居右
$dst_x=$fileinfo[0]-$wminfo[0];$dst_y=$fileinfo[1]-$wminfo[1];break;
default://随机
$dst_x=mt_rand(0,$fileinfo[0]-$wminfo[0]);$dst_y=mt_rand(0,$fileinfo[1]-$wminfo[1]);
}
if(function_exists('ImageAlphaBlending'))ImageAlphaBlending($temp_wm,True);//设定图像的混色模式
if(function_exists('ImageSaveAlpha'))ImageSaveAlpha($temp_wm,True);//保存完整的alpha通道信息
//为图像添加水印
if(function_exists('imageCopyMerge')){
ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);
}else{
ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);
}
//保存图片
switch($fileinfo['mime']){
case'image/jpeg':
@imageJPEG($temp,$file);
break;
case'image/png':
@imagePNG($temp,$file);
break;
case'image/gif':
@imageGIF($temp,$file);
break;
}
//销毁零时图像
@imageDestroy($temp);
@imageDestroy($temp_wm);
}
}
}
//获取文件扩展名
functionfileext($filename){
returnstrtolower(substr(strrchr($filename,'.'),1,10));
}
}
?>
㈣ php中上传的图片 路径保存到数据库中 没有后缀。jpg
有一个临时文件名,你要先获取,然后再把临时文件复制到新的路径。
如果你需要按照某种规则重命名,你首先要获取到原文件的扩展名。
㈤ java怎么获取上传文件的后缀
给你个示例,应该看得懂吧
File f =new File("Test.txt");
String fileName=f.getName();
String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println(prefix);
}
㈥ tp5文件上传时怎么把图片路径和post数据一起存到数据库(图片使用ajax提交post数据使用form提交)
ajax上传图片成功后返回图片路径
前端再把这个路径存到一个隐藏字段里,form一起提交
㈦ php 验证上传的文件类型为图片,并获得文件的后缀名
以下是我上传了一个图片后显示的 $_FILES['filename']的信息
[filename] => Array
(
[name] => Winter.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php2jw7QX
[error] => 0
[size] => 105542
)
其中type是文件类型的minitype 表示方法,例如普通的HTML的类型是text/html
如果你想用扩展名的方式判断的话可以用以下代码:
<?php
#允许的文件扩展名
$allowed_types = array('jpg', 'gif', 'png');
$filename = $_FILES['filename']['name'];
#正则表达式匹配出上传文件的扩展名
preg_match('|\.(\w+)$|', $filename, $ext);
#print_r($ext);
#转化成小写
$ext = strtolower($ext[1]);
#判断是否在被允许的扩展名里
if(!in_array($ext, $allowed_types)){
die('不被允许的文件类型');
}
?>
㈧ thinkphp里上传的图片如何进行重命名,命名为自己想要的格式,它默认格式是时间戳的
TP文档上说明有这个参数,saveRule保存规则:
不为空的时候可以选择手册上说的几种命名规则,不同的场景使用不同的方式,只要加个判断就可以了。如果同名需要覆盖原文件的话添加属性:$upload->uploadReplace = true;