<?php session_start(); $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; $save_dir="images"; $file_current_dir=dirname(__FILE__).'/'.$save_dir; $filepath='http://'.$_SERVER['HTTP_HOST'].'/'.$save_dir;
//$filepath=dirname($url).'/'.$save_dir; 这个是支持子目录的 /** * [file_upload 文件上传函数,支持单文件,多文件] * Author: 程威明 * @param string $name input表单中的name * @param string $save_dir 文件保存路径,相对于当前目录 * @param array $allow_suffix 允许上传的文件后缀 * @return array array() { * ["status"]=> 全部上传成功为true,全部上传失败为false,部分成功为成功数量 * ["path"]=>array() {已成功的文件路径} * ["error"]=>array() {失败信息} * } */ function files_upload($name="photo",$filename='file',$save_dir="images",$allow_suffix=array('jpg','jpeg','gif','png'),$random_keys='A') { //如果是单文件上传,改变数组结构 if(!is_array($_FILES[$name]['name'])){ $list = array(); foreach($_FILES[$name] as $k=>$v){ $list[$k] = array($v); } $_FILES[$name] = $list; } $response = array(); $response['status'] = array(); $response['path'] = array(); $response['error'] = array(); //拼接保存目录 $save_dir = './'.trim(trim($save_dir,'.'),'/').'/'; //判断保存目录是否存在 if(!file_exists($save_dir)) { //不存在则创建 if(false==mkdir($save_dir,0777,true)) { $response['status'] = false; $response['error'][] = '文件保存路径错误,路径 "'.$save_dir.'" 创建失败'; } } $num = count($_FILES[$name]['tmp_name']); $success = 0; //循环处理上传 for($i=0;$i <$num;$i++) { //判断是不是post上传 if(!is_uploaded_file($_FILES[$name]['tmp_name'][$i])) { $response['error'][] = '非法上传,文件 "'.$_FILES[$name]['name'][$i].'" 不是post获得的'; continue; } //判断错误 if($_FILES[$name]['error'][$i]>0) { $response['error'][] = '文件 "'.$_FILES[$name]['name'][$i].'" 上传错误,error下标为 "'.$_FILES[$name]['error'][$i].'"'; continue; } //获取文件后缀 $suffix = ltrim(strrchr($_FILES[$name]['name'][$i],'.'),'.'); //判断后缀是否是允许上传的格式 if(!in_array($suffix,$allow_suffix)) { $response['error'][] = 'file "'.$_FILES[$name]['name'][$i].'" types that are not allowed to upload '; continue; } if(empty($_SESSION['array'][$filename])) { $_SESSION['array'][$filename]=array("A"=>"A","B"=>"B","C"=>"C","D"=>"D","E"=>"E",'F'=>"F",'G'=>"G",'H'=>"H",'I'=>"I",'J'=>"J",'K'=>"K"); } $random_keys=array_shift($_SESSION['array'][$filename]); unset($_SESSION['array'][$filename][$random_keys]); //得到上传后文件名 $new_file_name =$filename.$random_keys.'.'.$suffix; //拼接完整路径 $new_path = $save_dir.$new_file_name; //上传文件 把tmp文件移动到保存目录中 if(!move_uploaded_file($_FILES[$name]['tmp_name'][$i],$new_path)) { $response['error'][] = '文件 "'.$_FILES[$name]['name'][$i].'" 从临时文件夹移动到保存目录时发送错误'; continue; } //返回由图片文件路径组成的数组 $response['path'][] =$save_dir.$new_file_name; $success++; } if(0==$success){ $success = false; }elseif($success==$num){ $success = true; } $response['status'] = $success; return $response; } if(empty($_SESSION['filename'])){ $_SESSION['filename']=''; } //文件改名 if(@$_POST['action']=='rename') { $oldname=$_POST['oldname']; //$oldname=str_replace(substr($oldname,strrpos($oldname,'.')),'',$oldname); $oldname=$save_dir."/".$oldname; $newname=$save_dir."/".$_POST['newfilename'].'.'.$_POST['ext']; $result=rename($oldname,$newname); if($result==true){ header('Location: '.$url); } } //文件删除 if(@$_POST['action']=='del') { $result=0; $file = $save_dir."/".$_POST['filename']; echo $file; $result = @unlink($file) or die('dsd');; if ($result == true) { $result=1; } echo $result; } if(@$_POST['action']=='upload'){ $filename=$_POST['filename']; $_SESSION['filename']=$filename; $aa=files_upload('file',$filename,'images',array('jpg','jpeg','gif','png'),$random_keys); if($aa['status']==true){ header('Location: '.$url); } else { echo $aa['error'][0]; } } else { $dir = $file_current_dir; // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { $i=0; while (($file = readdir($dh)) !== false) { $size = intval((filesize($dir.'/'.$file)/1000)).'k'; list($filesname,$kzm)=explode(".",$file);//获取扩展名 if($kzm=="gif" or $kzm=="jpg" or $kzm=="JPG" or $kzm=="png") { //文件过滤 ?> filename: <?php echo $file;?> <?php echo $size;?> <img src="<?php echo $filepath.'/'.$file ;?> " width="50px" height="50px"> <?php echo $filepath.'/'.$file;?> <a onclick="return rename('<?php echo $i;?>','<?php echo $file;?>',)" href='#'>rename</a> <form method="post" id="form_<?php echo $i;?>" action="1.php" style="display:none"> <input type="text" name="newfilename" id="newfilename" > <input type="hidden" name="oldname" id="oldname" value="<?php echo $file;?>" > <input type="hidden" name="action" id="action" value="rename"> <input type="hidden" name="ext" id="ext" value="<?php echo $kzm;?>"> <input type="submit" name="submit" value="Submit" /> </form> <a onclick="return deldel('<?php echo $file;?>')" href='#' >del</a> <br> <?php $i++; } } closedir($dh); } } ?> <html> <body> <br> <br> <br> <form action="1.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <label for="file">name</label> <input type="text" name="filename" id="filename" value="<?php echo $_SESSION['filename'];?>"> <input type="hidden" name="action" value="upload"> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php } ?> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script> function rename(formid,filename) { $("#form_"+formid).toggle(); } function deldel(filename) { var del='del'; if(confirm('confirm del')){ $.ajax({ type:'post', url:'<?php echo $url;?>', data: {action:del, filename:filename}, success:function(data){ alert('del success'); location.reload(); }, error:function(){ alert('del fail,check file mod'); } }); } } </script>